使用JAXB将XML字符串解组为Java对象

使用JAXB将XML字符串解组为Java对象,java,xml,jaxb,xml-namespaces,Java,Xml,Jaxb,Xml Namespaces,我想使用JAXB将下面的XML字符串转换为Java对象 我可以转换对象,但在解组后,文档将显示为null。 Result[hits=1,tookInMillis=10,totalHits=1,documents=null] 如何更正文档对象以获取值 XML字符串: <result hits="1" tookInMillis="9" totalHits="1" xmlns="http://www.example.com/search/result/1.0"> <docume

我想使用JAXB将下面的XML字符串转换为Java对象

我可以转换对象,但在解组后,
文档
将显示为
null
Result[hits=1,tookInMillis=10,totalHits=1,documents=null]
如何更正
文档
对象以获取值

XML字符串:

<result hits="1" tookInMillis="9" totalHits="1" xmlns="http://www.example.com/search/result/1.0">
   <documents>
      <document id="1" company="TEST" type="CN" generationDate="2018-05-24T06:05:37.000Z">
         <field type="xs:string" name="test1">test1</field>
         <field type="xs:string" name="test2">test2</field>
         <field type="xs:string" name="test3">test3</field>
         <field type="xs:string" name="test4">test4</field>
         <field type="xs:string" name="test5">test5</field>
         <field type="xs:string" name="test6">test6</field>
         <field type="xs:string" name="test7">test7</field>
         <field type="xs:string" name="test8">test8</field>
         <field type="xs:date" name="date">2018-05-23</field>
      </document>
   </documents>
</result>

测试1
测试2
测试3
测试4
测试5
测试6
测试7
测试8
2018-05-23

您需要小心使用XML名称空间

在XML中,XML元素中给定的名称空间(如
) 继承到其子元素(

在Java中,它不是。因此,您需要显式地给出名称空间 在子属性的
@xmlement
@xmlementwrapper
注释中

在对XML示例进行解组时,以下Java类可以很好地工作。 特别是,集合
结果.documents
文档.fields
不要以
null
的形式出现

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "result", namespace = "http://www.example.com/search/result/1.0")
public class Result {

    @XmlAttribute
    private int hits;

    @XmlAttribute
    private int tookInMillis;

    @XmlAttribute
    private int totalHits;

    @XmlElementWrapper(name = "documents", namespace = "http://www.example.com/search/result/1.0")
    @XmlElement(name = "document", namespace = "http://www.example.com/search/result/1.0")
    private List<Document> documents;

    // ... public getters and setters (omitted for brevity)
}
@xmlacessortype(xmlacesstype.FIELD)
@XmlRootElement(name=“result”,命名空间=”http://www.example.com/search/result/1.0")
公开课成绩{
@XmlAttribute
私人整数点击;
@XmlAttribute
图金米利斯私人酒店;
@XmlAttribute
私人整数总点击数;
@XmlElementWrapper(name=“documents”,命名空间=”http://www.example.com/search/result/1.0")
@XmlElement(name=“document”,命名空间=”http://www.example.com/search/result/1.0")
私人清单文件;
//…公共getter和setter(为简洁起见省略)
}
@xmlacessortype(xmlacesstype.FIELD)
公共类文档{
@XmlAttribute
私有int-id;
@XmlAttribute
私人弦公司;
@XmlAttribute
私有字符串类型;
@XmlAttribute
私有日期生成日期;
@XmlElement(name=“field”,命名空间=”http://www.example.com/search/result/1.0")
私有列表字段;
//…公共getter和setter(为简洁起见省略)
}

XML不足以回答这个问题。还请发布您的POJO类和代码,将XML解压到POJO。它正在工作,我能够获取文档对象值。非常感谢:)我没有为列表文档和列表字段添加名称空间,正如您在添加名称空间后提到的,它工作正常。
@XmlAccessorType(XmlAccessType.FIELD)
public class Document {

    @XmlAttribute
    private int id;

    @XmlAttribute
    private String company;

    @XmlAttribute
    private String type;

    @XmlAttribute
    private Date generationDate;

    @XmlElement(name = "field", namespace = "http://www.example.com/search/result/1.0")
    private List<Field> fields;

    // ... public getters and setters (omitted for brevity)
}