java JAXB - 未编组的字段为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15085648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAXB - unmarshalled fields are null
提问by pablisco
We are unmarshalling a response from http://xmlgw.companieshouse.gov.uk/. This is the text sent to the marshall:
我们正在解组来自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>
The model of CoSearchItem is like this:
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 model has this structure:
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
}
The package has this annotations:
该包具有以下注释:
@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;
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.
解组是从第一个Node
从更大的Document
,any
项目列表中提取的。当我们解析 xml 时,CoSearchItem 中的所有字段都设置为 null 并且无法找出原因。
回答by bdoughan
You need to use a package level @XmlSchema
annotation to specify the namespace qualification for your model.
您需要使用包级别@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;
This this specified you do not require to specify the namespace URI on the @XmlRootElement
and @XmlType
on your NameSearch
class.
这指定了您不需要在@XmlRootElement
和@XmlType
上指定名称空间 URI NameSearch
。
For More Information
想要查询更多的信息
The unmarshaling is done from the first Node extracted from a larger Document, inside an any list of items.
解组是从任何项目列表中从较大文档中提取的第一个节点完成的。
Make sure the DOM parer used to create the nodes is namespace aware.
确保用于创建节点的 DOM parer 是命名空间感知的。
documentBuilderFactory.setNamespaceAware(true);
回答by pablisco
I figured out the correct answer thanks to @Blaise Doughan. After looking at the package namespace qualification I found that it was pointing to:
感谢@ Blaise Doughan,我想出了正确的答案。查看包命名空间限定后,我发现它指向:
"http://xmlgw.companieshouse.gov.uk/v1-0"
and it should have been pointing to:
它应该指向:
"http://xmlgw.companieshouse.gov.uk/v1-0/schema"
Not sure how that got misplaced.
不知道那是怎么放错地方的。
回答by Prateek Mehta
I solved this by making elementFormDefault="unqualified"
in the xsd before generating stubs, else make the change manually in package-info.java
我通过elementFormDefault="unqualified"
在生成存根之前在 xsd 中制作来解决这个问题,否则在 package-info.java 中手动进行更改