JAXB - unmarshalled字段为null [英] JAXB - unmarshalled fields are null
问题描述
我们正在解析 http://xmlgw.companieshouse.gov.uk/ 的回复。这是发送给马歇尔的文本:
We are unmarshalling a response from http://xmlgw.companieshouse.gov.uk/. This is the text sent to the marshall:
<NameSearch xmlns="http://xmlgw.companieshouse.gov.uk/v1-0/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlgw.companieshouse.gov.uk/v1-0/schema http://xmlgw.companieshouse.gov.uk/v1-0/schema/NameSearch.xsd">
<ContinuationKey>...</ContinuationKey>
<RegressionKey>...</RegressionKey>
<SearchRows>20</SearchRows>
<CoSearchItem>
<CompanyName>COMPANY NAME</CompanyName>
<CompanyNumber>23546457</CompanyNumber>
<DataSet>LIVE</DataSet>
<CompanyIndexStatus>DISSOLVED</CompanyIndexStatus>
<CompanyDate></CompanyDate>
</CoSearchItem>
// more CoSearchItem elements
</NameSearch>
CoSearchItem的模型如下:
The model of CoSearchItem is like this:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "CoSearchItem", propOrder = {
"companyName",
"companyNumber",
"dataSet",
"companyIndexStatus",
"companyDate",
"searchMatch"
})
public class CoSearchItem {
@XmlElement(name = "CompanyName", required = true)
protected String companyName;
@XmlElement(name = "CompanyNumber", required = true)
protected String companyNumber;
@XmlElement(name = "DataSet", required = true)
protected String dataSet;
@XmlElement(name = "CompanyIndexStatus")
protected String companyIndexStatus;
@XmlElement(name = "CompanyDate")
@XmlSchemaType(name = "date")
protected XMLGregorianCalendar companyDate;
@XmlElement(name = "SearchMatch")
protected String searchMatch;
// getters and setters
}
NameSearch模型具有以下结构:
NameSearch model has this structure:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "NameSearch", namespace = "http://xmlgw.companieshouse.gov.uk/v1-0/schema", propOrder = {
"continuationKey",
"regressionKey",
"searchRows",
"coSearchItem"
})
@XmlRootElement(name = "NameSearch", namespace = "http://xmlgw.companieshouse.gov.uk/v1-0/schema")
public class NameSearch {
@XmlElement(name = "ContinuationKey", required = true)
protected String continuationKey;
@XmlElement(name = "RegressionKey", required = true)
protected String regressionKey;
@XmlElement(name = "SearchRows", required = true)
protected BigInteger searchRows;
@XmlElement(name = "CoSearchItem")
protected List<CoSearchItem> coSearchItem;
// setters and getters
}
包有这样的注释:
@XmlSchema(namespace = "http://xmlgw.companieshouse.gov.uk/v1-0", elementFormDefault = XmlNsForm.QUALIFIED, //
xmlns = {
@XmlNs(prefix = "xsi", namespaceURI = "http://www.w3.org/2001/XMLSchema-instance")
}
)
package uk.gov.companieshouse;
解编是从第一个节点
完成的从任何
项目列表中的较大文档
中提取。当我们解析xml但是CoSearchItem中的所有字段都设置为null并且无法找出原因。
The unmarshaling is done from the first Node
extracted from a larger Document
, inside an any
list of items. When we parse the xml however all the fields in CoSearchItem are set to null and can't figure out the reason.
推荐答案
你需要使用包级别 @XmlSchema
注释来指定模型的命名空间限定。
You need to use a package level @XmlSchema
annotation to specify the namespace qualification for your model.
@XmlSchema(
namespace = "http://xmlgw.companieshouse.gov.uk/v1-0/schema",
elementFormDefault = XmlNsForm.QUALIFIED)
package example;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
这个指定您不需要在 @上指定名称空间URI您的
和 NameSearch
类中的XmlRootElement @XmlType
。
This this specified you do not require to specify the namespace URI on the @XmlRootElement
and @XmlType
on your NameSearch
class.
更多信息
- http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
解组是从第一个从较大的
文档中提取的节点完成的。项目列表。
The unmarshaling is done from the first Node extracted from a larger Document, inside an any list of items.
确保用于创建节点的DOM parer是名称空间感知的。
Make sure the DOM parer used to create the nodes is namespace aware.
documentBuilderFactory.setNamespaceAware(true);
这篇关于JAXB - unmarshalled字段为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!