用户登录
用户注册

分享至

JAXB - 未编组的字段为空

  • 作者: Meng_surper
  • 来源: 51数据库
  • 2022-11-30

问题描述

我们正在整理来自 http://www.51sjk.com/Upload/Articles/1/0/337/337526_20221130102651108.jpg 的回复.这是发给马歇尔的文字:

We are unmarshalling a response from http://www.51sjk.com/Upload/Articles/1/0/337/337526_20221130102651108.jpg. This is the text sent to the marshall:

<NameSearch xmlns="http://www.51sjk.com/Upload/Articles/1/0/337/337526_20221130102651108.jpgv1-0/schema" xmlns:xsi="http://www.51sjk.com/Upload/Articles/1/0/337/337526_20221130102652397.jpg" xsi:schemaLocation="http://www.51sjk.com/Upload/Articles/1/0/337/337526_20221130102651108.jpgv1-0/schema http://www.51sjk.com/Upload/Articles/1/0/337/337526_20221130102651108.jpgv1-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://www.51sjk.com/Upload/Articles/1/0/337/337526_20221130102651108.jpgv1-0/schema", propOrder = {
    "continuationKey",
    "regressionKey",
    "searchRows",
    "coSearchItem"
})
@XmlRootElement(name = "NameSearch", namespace = "http://www.51sjk.com/Upload/Articles/1/0/337/337526_20221130102651108.jpgv1-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://www.51sjk.com/Upload/Articles/1/0/337/337526_20221130102651108.jpgv1-0", elementFormDefault = XmlNsForm.QUALIFIED, //
    xmlns = {
        @XmlNs(prefix = "xsi", namespaceURI = "http://www.51sjk.com/Upload/Articles/1/0/337/337526_20221130102652397.jpg") 
     }
)

package uk.gov.companieshouse;

解组是从一个较大的Document中提取的第一个Node完成的,在any项目列表中.然而,当我们解析 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://www.51sjk.com/Upload/Articles/1/0/337/337526_20221130102651108.jpgv1-0/schema",
    elementFormDefault = XmlNsForm.QUALIFIED)
package example;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

这表明您不需要在 @XmlRootElement 和 @XmlType 上指定名称空间 URI.NameSearch 类.

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 解析器是命名空间感知的.

Make sure the DOM parer used to create the nodes is namespace aware.

documentBuilderFactory.setNamespaceAware(true);
软件
前端设计
程序设计
Java相关