JAXB - unmarshalled字段为null
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAXB - unmarshalled字段为null相关的知识,希望对你有一定的参考价值。
我们正在解密http://xmlgw.companieshouse.gov.uk/的回复。这是发送给马歇尔的文字:
<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的模型是这样的:
@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模型具有以下结构:
@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;
解组是从第一个从更大的Node
中提取的Document
完成的,在any
项目列表中。当我们解析xml但是CoSearchItem中的所有字段都设置为null并且无法找出原因。
您需要使用包级别@XmlSchema
批注来指定模型的命名空间限定。
@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;
这指定您不需要在@XmlRootElement
类的@XmlType
和NameSearch
上指定名称空间URI。
欲获得更多信息
解组是从任何项目列表中的较大文档中提取的第一个节点完成的。
确保用于创建节点的DOM parer是名称空间感知的。
documentBuilderFactory.setNamespaceAware(true);
由于@Blaise Doughan,我找到了正确的答案。在查看包命名空间限定后,我发现它指向:
"http://xmlgw.companieshouse.gov.uk/v1-0"
它应该指向:
"http://xmlgw.companieshouse.gov.uk/v1-0/schema"
不确定这是怎么放错位置的。
我通过在生成存根之前在xsd中创建elementFormDefault="unqualified"
来解决这个问题,否则在package-info.java中手动进行更改
以上是关于JAXB - unmarshalled字段为null的主要内容,如果未能解决你的问题,请参考以下文章
NoClassDefFoundError:由于缺少依赖关系,无法加载类groovy.xml.jaxb.JaxbGroovyMethods javax / xml / bind / Unmarshall