2013-02-26 73 views
4

我們正在編組來自http://xmlgw.companieshouse.gov.uk/的響應。這是發送到馬歇爾文字:JAXB - 未編組字段爲空

<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,並且無法找出原因。

+1

does [this](http://stackoverflow.com/questions/12747484/jaxb-parses-xml-child-into-null-only-the-attributes-are-set-in-the-object)有幫助嗎? – TheWhiteRabbit 2013-02-26 09:52:50

+0

它指向像Blaise這樣的@XmlSchema,就是這樣。在發現不正確的命名空間限定後,我發現了這個問題。 – pablisco 2013-02-26 10:13:44

回答

13

您需要使用包級別@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); 
+1

感謝您指點我的包模式註釋:)事實證明,這不是正確的。 – pablisco 2013-02-26 10:11:57

+3

在1.8.0_100左右的Java中發生了一個變化,這個答案是爲所有字段設置模式的唯一正確方法。在該版本之前@XmlRootElement也在工作。 – Sankozi 2016-12-19 10:13:20

2

由於@Blaise Doughan,我找到了正確的答案。看包的命名空間的資格後,我發現它是指向:

"http://xmlgw.companieshouse.gov.uk/v1-0" 

,它應該已經指向:

"http://xmlgw.companieshouse.gov.uk/v1-0/schema" 

不知道如何得到錯誤的。

+1

這將做到這一點。發生的事情是,JAXB認爲每個'@ XmlElement'註釋都在錯誤的名稱空間中,所以在反序列化時它們不匹配,所以數據在地面上丟失。 (JAXB默認忽略未知元素。) – 2013-02-26 10:26:05