2

我有

@XmlRootElement(namespace = "http://www.w3.org/2005/Atom", name = "content")
@XmlType(name = "course")
public class Course implements Resource

...

@XmlElementWrapper(name="subcourses")
@XmlElement(name="course")
List<Xlink> subcourses;         //!?

和 Xlink 类,它在内联变量中工作正常。

public class Xlink
{
    private String href;
    private String value;

    @XmlAttribute(namespace = "http://www.w3.org/1999/xlink")
    public String getHref()
    {
        return href;
    }

    public void setHref(String href)
    {
        this.href = href;
    }

    @XmlValue
    public String getValue()
    {
        return value;
    }

    public void setValue(String value)
    {
        this.value = value;
    }
}

对于 XML 输入

<atom:content atom:type="xml" xsi:type="course">
...
   <subcourses>
      <course xlink:href="course1">Some course</course>
      <course xlink:href="course2">other course</course>

并且 subcourses 拒绝被解组(没有任何异常被抛出)。

注意:很遗憾,MOXy 不是一个选项。

编辑:新编组对象

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:content xmlns:ns2="http://www.w3.org/1999/xlink" xmlns:ns3="http://www.w3.org/2005/Atom">
    <code>SOME CODE</code>
    <name>name</name>
    <subcourses>
        <course ns2:href="Some href">some value</course>
        <course ns2:href="sdsdg">sdfhdfhdhdh</course>
    </subcourses>
</ns3:content>

Edit2:经过一些解组和编组测试对象的实验后,我发现我需要在内容的标题中定义 xmlns 命名空间以匹配问题是我从解析出来的包装类内部获取 Coursexlink:href=元素xmlns:xlink="http://www.w3.org/1999/xlink"通过resteasy。因此,生成的类不会传递命名空间信息。

我不知何故需要强迫 JAXB 理解这xmlns:xlink="http://www.w3.org/1999/xlink"适用于课程元素,但经过一个小时的谷歌搜索后,我不知所措。

Edit3:我从

https://github.com/jirutka/atom-jaxb/blob/master/src/main/java/cz/jirutka/atom/jaxb/Entry.java

在服务器对应方上使用。这又是

https://github.com/jirutka/atom-jaxb/blob/master/src/main/java/cz/jirutka/atom/jaxb/Feed.java

我的解组代码的相关部分是:

Feed f = r.readEntity(Feed.class);
out.addAll(unmarshaller.Unmarshal(f.getEntries(), clazz));

哪里rjavax.ws.rs.core.Response。和解组器

public List<T> Unmarshal(List<Entry> entries, Class clazz)
{
    List<T> out = new ArrayList<T>();
    T instance;
    for (Entry e : entries)
    {
        try
        {
            JAXBContext context = JAXBContext.newInstance(clazz);
            Unmarshaller unmarsh = context.createUnmarshaller();
            instance = (T) unmarsh.unmarshal((Node) e.getContent());

由于这是我第一次接触这项技术,所以这段代码完全有可能是“wtf”。

4

1 回答 1

8

当你注释字段(实例变量)时,一定要放在@XmlAccessorType(XmlAccessType.FIELD)你的类上。

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement(namespace = "http://www.w3.org/2005/Atom", name = "content")
@XmlAccessorType(XmlAccessType.FIELD)
public class Course implements Resource {

    @XmlElementWrapper(name = "subcourses")
    @XmlElement(name = "course")
    List<Xlink> subcourses;

}

然后确保您的 XML 输入是正确的命名空间限定的。您的输入文档应如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<atom:content xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:atom="http://www.w3.org/2005/Atom">
    <code>SOME CODE</code>
    <name>name</name>
    <subcourses>
        <course xlink:href="Some href">some value</course>
        <course xlink:href="sdsdg">sdfhdfhdhdh</course>
    </subcourses>
</atom:content>

使用我更新Course的类、您的Xlink类和正确命名空间限定的 XML 文档,以下演示代码非常适合我。

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Course.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum17766166/input.xml");
        Course course = (Course) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(course, System.out);
    }

}

更新#1

Edit2:在对测试对象进行解组和编组实验后,我发现我需要在内容的标题中定义 xmlns 命名空间以匹配 xlink:href= 像 xmlns:xlink="http://www.w3.org/ 1999/xlink”的问题是我从一个包装类中获取课程元素,该包装类被resteasy解析出来。因此,生成的类不会传递命名空间信息。

解决问题的最佳位置是提取要解组的片段。以下是您可以与 StAX 一起使用的策略。

输入.xml

下面是一个示例 XML 文档,其中在您希望解组的片段上方定义了名称空间信息。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:atom="http://www.w3.org/2005/Atom">
    <bar>
        <atom:content>
            <code>SOME CODE</code>
            <name>name</name>
            <subcourses>
                <course xlink:href="Some href">some value</course>
                <course xlink:href="sdsdg">sdfhdfhdhdh</course>
            </subcourses>
        </atom:content>
    </bar>
</foo>

演示

下面我们将使用 StAXXMLStreamReader导航到目标片段。我们将让我们的 JAXB 实现解组这个片段。这样所有的命名空间信息都被保留了。

import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Course.class);

        XMLInputFactory xif = XMLInputFactory.newFactory();
        StreamSource source = new StreamSource("src/forum17766166/input.xml");
        XMLStreamReader xsr = xif.createXMLStreamReader(source);
        while(xsr.hasNext()) {
            if(xsr.isStartElement() && "content".equals(xsr.getLocalName())) {
                break;
            }
            xsr.next();
        }

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Course course = (Course) unmarshaller.unmarshal(xsr);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(course, System.out);
    }

}

输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:content xmlns:ns2="http://www.w3.org/1999/xlink" xmlns:ns3="http://www.w3.org/2005/Atom">
    <subcourses>
        <course ns2:href="Some href">some value</course>
        <course ns2:href="sdsdg">sdfhdfhdhdh</course>
    </subcourses>
</ns3:content>

更新#2

如果您无法按照 UPDATE #1 中的说明生成更好的 XML 片段,那么您可以通过以下方法修复您当前拥有的 XML 片段。

命名空间过滤器

您可以使用 SAXXMLFilter来修复您的 XML 文档。

import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class NamespaceFilter extends XMLFilterImpl {

    private static final String ATOM_URI = "http://www.w3.org/2005/Atom";
    private static final String XLINK_URI = "http://www.w3.org/1999/xlink";

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes atts) throws SAXException {
        if("atom:content".equals(qName)) {
            super.startElement(ATOM_URI, "content", qName, atts);
        } else if("course".equals(qName))  {
            AttributesImpl modifiedAtts = new AttributesImpl();
            modifiedAtts.addAttribute(XLINK_URI, "href", "xlink:href", null, atts.getValue(0));
            super.startElement(uri, localName, qName, modifiedAtts);
        } else {
            super.startElement(uri, localName, qName, atts);
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        if("atom:content".equals(qName)) {
            super.endElement(ATOM_URI, "content", qName);
        } else {
            super.endElement(uri, localName, qName);
        }
    }

}

演示

以下是如何利用XmlFilterJAXB:

import javax.xml.bind.*;
import javax.xml.parsers.*;
import org.xml.sax.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Course.class);

        // Create the XMLFilter
        XMLFilter filter = new NamespaceFilter();

        // Set the parent XMLReader on the XMLFilter
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        filter.setParent(xr);

        // Set UnmarshallerHandler as ContentHandler on XMLFilter
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        UnmarshallerHandler unmarshallerHandler = unmarshaller
                .getUnmarshallerHandler();
        filter.setContentHandler(unmarshallerHandler);

        // Parse the XML
        InputSource xml = new InputSource("src/forum17766166/input.xml");
        filter.parse(xml);
        Course course = (Course) unmarshallerHandler.getResult();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(course, System.out);
    }

}

了解更多信息


更新#3

下面是您的示例代码的简化版本,一切正常。也许您的代码中有一些不同的东西会帮助您找到。

入口

import javax.xml.bind.annotation.*;

@XmlRootElement(namespace="http://www.w3.org/2005/Atom")
@XmlAccessorType(XmlAccessType.FIELD)
public class Entry<T> {

    @XmlElement(namespace = "http://www.w3.org/2005/Atom")
    @XmlSchemaType(name = "atomInlineOtherContent")
    private T content;

    public T getContent() {
        return content;
    }

}

输入.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<atom:entry xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:atom="http://www.w3.org/2005/Atom">
    <atom:content>
        <code>SOME CODE</code>
        <name>name</name>
        <subcourses>
            <course xlink:href="Some href">some value</course>
            <course xlink:href="sdsdg">sdfhdfhdhdh</course>
        </subcourses>
    </atom:content>
</atom:entry>

演示

import java.io.File;
import javax.xml.bind.*;
import org.w3c.dom.Node;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Entry.class, Course.class);

        // Unmarshal Entry
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum17766166/input.xml");
        Entry entry = (Entry) unmarshaller.unmarshal(xml);

        // Unmarshal Course
        Node contentNode = (Node) entry.getContent();
        Course course = (Course) unmarshaller.unmarshal(contentNode);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(course, System.out);
    }

}

输出

<?xml version="1.0" encoding="UTF-8"?>
<ns0:content xmlns:ns1="http://www.w3.org/1999/xlink" xmlns:ns0="http://www.w3.org/2005/Atom">
   <subcourses>
      <course ns1:href="Some href">some value</course>
      <course ns1:href="sdsdg">sdfhdfhdhdh</course>
   </subcourses>
</ns0:content>
于 2013-07-20T22:42:54.217 回答