当前位置: 首页 > 知识库问答 >
问题:

生成的XML中的子元素没有Parent的命名空间-JAXB

云炜
2023-03-14

我正在使用JAXB生成用于编组/解组XML的类。问题是父类的命名空间仅适用于XML中的父元素,而不适用于JAXB注释类中使用的子类。

我不知道我犯了什么错误。下面是我的xsd和xml以及类。

XSD:

项目Responses.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns="http://w.com/"
        targetNamespace="http://w.com/"
        elementFormDefault="qualified"
        version="1.3.2">

        <xsd:include schemaLocation="ItemCommons.xsd"/>

        <xsd:element name="ItemResponses" type="ItemResponses"/>

        <xsd:complexType name="ItemResponses">
            <xsd:sequence>
                <xsd:element name="ItemResponse" type="ItemResponse" 
        minOccurs="1" maxOccurs="10000"/>
                <xsd:element name="additionalAttributes" 
        type="NameValueAttributes" minOccurs="0">
                    <xsd:annotation>
                        <xsd:documentation>
                            additional attributes bag, for future use
                        </xsd:documentation>
                    </xsd:annotation>
                </xsd:element>
                <xsd:element name="totalItems" type="xsd:integer" 
minOccurs="0" maxOccurs="1"/>
                <xsd:element name="nextCursor" type="xsd:string" 
minOccurs="0" maxOccurs="1"/>
            </xsd:sequence>
        </xsd:complexType>

        <xsd:simpleType name="ItemPublishStatus">
            <xsd:restriction base="xsd:string">
                <xsd:enumeration value="IN_PROGRESS"/>
                <xsd:enumeration value="READY_TO_PUBLISH"/>
                <xsd:enumeration value="PUBLISHED"/>
                <xsd:enumeration value="UNPUBLISHED"/>
                <xsd:enumeration value="STAGE"/>
                <xsd:enumeration value="SYSTEM_PROBLEM"/>
            </xsd:restriction>
        </xsd:simpleType>

        <xsd:complexType name="ItemResponse">
            <xsd:sequence>
                <xsd:element name="mart" type="Mart" minOccurs="0">
                    <xsd:annotation>
                        <xsd:documentation>
                            mart where the item would be listed
                        </xsd:documentation>
                    </xsd:annotation>
                </xsd:element>
                <xsd:element name="sku" minOccurs="1">
                    <xsd:annotation>
                        <xsd:documentation>
                            partner's item identifier
                        </xsd:documentation>
                    </xsd:annotation>
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="255"/>
                            <xsd:minLength value="1"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="wpid" minOccurs="0">
                    <xsd:annotation>
                        <xsd:documentation>
                            w's item identifier
                        </xsd:documentation>
                    </xsd:annotation>
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="12"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="upc" minOccurs="0">
                    <xsd:annotation>
                        <xsd:documentation>
                            UPC
                        </xsd:documentation>
                    </xsd:annotation>
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="14"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="gtin" minOccurs="0">
                    <xsd:annotation>
                        <xsd:documentation>
                        </xsd:documentation>
                    </xsd:annotation>
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="1000"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="productName" minOccurs="0">
                    <xsd:annotation>
                        <xsd:documentation>
                            Title of the product to be displayed on the 
product details page.
                        </xsd:documentation>
                    </xsd:annotation>
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="200"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="shelf" minOccurs="0" maxOccurs="1">
                    <xsd:annotation>
                        <xsd:documentation>
                        </xsd:documentation>
                    </xsd:annotation>
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="1000"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="productType" minOccurs="0" 
maxOccurs="1">
                    <xsd:annotation>
                        <xsd:documentation>
                        </xsd:documentation>
                    </xsd:annotation>
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="1000"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="price" type="Money" minOccurs="0">
                    <xsd:annotation>
                        <xsd:documentation>
                            current price
                        </xsd:documentation>
                    </xsd:annotation>
                </xsd:element>
                <xsd:element name="publishedStatus" 
type="ItemPublishStatus" minOccurs="0">
                    <xsd:annotation>
                        <xsd:documentation>
                            item publish status
                        </xsd:documentation>
                    </xsd:annotation>
                </xsd:element>
                <xsd:element name="additionalAttributes" 
type="NameValueAttributes" minOccurs="0">
                    <xsd:annotation>
                        <xsd:documentation>
                            additional attributes bag, for future use
                        </xsd:documentation>
                    </xsd:annotation>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
        <xsd:complexType name="NameValueAttributes">
            <xsd:sequence>
                <xsd:element name="name" type="xsd:string" 
minOccurs="1">
                    <xsd:annotation>
                        <xsd:documentation>
                            name
                        </xsd:documentation>
                    </xsd:annotation>
                </xsd:element>
                <xsd:element name="type" type="ValueType" 
minOccurs="1">
                    <xsd:annotation>
                        <xsd:documentation>
                            type
                        </xsd:documentation>
                    </xsd:annotation>
                </xsd:element>
                <xsd:element name="value" type="AttributeValue" 
minOccurs="1" maxOccurs="unbounded">
                    <xsd:annotation>
                        <xsd:documentation>
                            Set of values for the attribute
                        </xsd:documentation>
                    </xsd:annotation>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
        <xsd:complexType name="AttributeValue">
            <xsd:sequence>
                <xsd:element name="value" type="xsd:string" 
minOccurs="1">
                    <xsd:annotation>
                        <xsd:documentation>
                            value expressed in string
                        </xsd:documentation>
                    </xsd:annotation>
                </xsd:element>
                <xsd:element name="group" type="xsd:string" 
 minOccurs="0">
                    <xsd:annotation>
                        <xsd:documentation>
                            list of values
                        </xsd:documentation>
                    </xsd:annotation>
                </xsd:element>
                <xsd:element name="rank" type="xsd:int" minOccurs="0">
                    <xsd:annotation>
                        <xsd:documentation>
                            list of values
                        </xsd:documentation>
                    </xsd:annotation>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:schema>

JAXB类

项目响应.java

@XmlRootElement(name="ItemResponses")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ItemResponses", propOrder = {
"itemResponse",
"additionalAttributes",
"totalItems",
"nextCursor"
})
public class ItemResponses extends GatewayBaseEntity {

    @XmlElement(name = "ItemResponse", required = true)
    protected List<ItemResponse> itemResponse;
    @XmlElement
    protected NameValueAttributes additionalAttributes;
    @XmlElement(required = false)
    protected long totalItems;
    @XmlElement(required = false)
    protected String nextCursor;
    ... getters & Setters.
}

项目响应.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ItemResponse", propOrder = {
"mart",
"sku",
"wpid",
"upc",
"gtin",
"productName",
"shelf",
"productType",
"price",
"publishedStatus",
"additionalAttributes"
})
@XmlRootElement(name = "ItemResponse")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class ItemResponse {

@XmlSchemaType(name = "string")
protected Mart mart;
@XmlElement(required = true)
protected String sku;
@XmlElement
protected String wpid;
@XmlElement
protected String upc;
@XmlElement
protected String gtin;
@XmlElement
protected String productName;
@XmlElement
protected String shelf;
@XmlElement
protected String productType;
@XmlElement
protected Money price;
@XmlElement
protected String publishedStatus;
@XmlElement
protected NameValueAttributes additionalAttributes;
... getters & Setters.
}

Money.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Money", propOrder = {
"currency",
"amount"
})
public class Money {

@XmlElement(required = true)
protected CurrencyCode currency;
@XmlElement(required = true)
protected BigDecimal amount;
...getters & setters ..
}

XML输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:ItemResponses xmlns:ns2="http://w.com/">
<ns2:ItemResponse>
    <ns2:mart>w_US</ns2:mart>
    <ns2:sku>382</ns2:sku>
    <ns2:wpid>0RC</ns2:wpid>
    <ns2:upc>689</ns2:upc>
    <ns2:gtin>006</ns2:gtin>
    <ns2:productName>LAM 24 x 36</ns2:productName>
    <ns2:productType>os</ns2:productType>
    <ns2:price>
        <currency>USD</currency> <!-- PROBLEM HERE -->
        <amount>24.99</amount>   <!-- PROBLEM HERE -->
    </ns2:price>
    <ns2:publishedStatus>PUBLISHED</ns2:publishedStatus>
</ns2:ItemResponse>
<ns2:totalItems>87</ns2:totalItems>
    <ns2:nextCursor>AoE/G==</ns2:nextCursor>
</ns2:ItemResponses>

包信息。java有以下条目

@javax.xml.bind.annotation.XmlSchema(
    xmlns = {
            @javax.xml.bind.annotation.XmlNs(prefix = "ns2", 
            namespaceURI="http://walmart.com/"),
            @javax.xml.bind.annotation.XmlNs(prefix = "wmSpecs", 
            namespaceURI ="http://w.com"),
            @javax.xml.bind.annotation.XmlNs(prefix = "xsi", 
            namespaceURI = "http://www.w3.org/2001/XMLSchema-instance")
    },
    namespace = "http://w.com/",
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
    package com.w.services.item.setup.query.domain.model.v3;

货币类别不在同一个包中。如何将其包含在 Jaxb 类中并生成 xml?

共有1个答案

谢同化
2023-03-14

添加:

@javax.xml.bind.annotation.XmlSchema(
   namespace = "http://w.com/",
   elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)

对应包的< code>package-info.java。

确保“钱”在同一个包中。

在您的模式中,我还缺少< code > target namespace = " http://w . com/" 。

 类似资料:
  • 我使用CXF从WSDL/XSD生成java类,然后返回XML(用于JMS)。 在生成的一个类中,它表示:

  • 我正在从jaxb模型生成rest响应(使用Jersey)。对于某些响应,生成的XML将名称空间前缀(ns2)添加到名称空间属性中,尽管它们都存在于同一个名称空间中。但对其他人来说,这完全没问题。 根据我的分析,我认为当一个复杂元素(另一个jaxb模型)在其中使用时会发生这种情况。但所有这些模型都在package-info.java中的相同名称空间中声明。 这是代码。 XYZModel.class

  • 我有一个有趣的情况,我的XML编辑器(Oxygen,使用Xerces XML处理器)需要根标签上的前缀,但是我的JAXB XML Marshaller(也是基于Xerces的)不需要根标签上的前缀。我正在试图理解这种情况。 首先是2个模式文件: ns1.xsd ns2。xsd 目前的氧气释放(16.1)需要我称之为“版本1”的东西 版本1 如果我像下面的示例(版本1)那样删除前缀: 版本2 氧气抱

  • 我正在尝试使用 JAXB 创建站点地图索引文件。按照创建站点地图的要求,我必须在根元素中添加命名空间属性: 我想有一个简单的方法来解决这个问题。因为这似乎是一个标准过程,所以我不想做复杂的解决方法,也不想在我的项目中添加更多的依赖项来解决这个问题 当前输出如下: 我的< code>SitemapIndex模型如下: 我还尝试手动添加名称空间字段,它可以生成文件,但是当我尝试读取文件时会出现异常。

  • 我使用JAXB根据定义的模式封送/解封xml文档。我注意到的是,JAXB在编组的xml中生成了不正确的名称空间。 以下是详细信息- soap.xsd-- 方案1.xsd-- schema2.xsd-- 我为jaxbMarshaller(在我的Spring配置中)定义了一个命名空间Prefix MapperImpl,它将URI映射到我定义的前缀名称。 编组的响应xml如下所示。请注意,JAXB在命名

  • 我有几个关于JAXB编组的简单问题。我正在尝试封送包含以下字段的类: 只需使用以下序列化代码: 我得到的输出是: 现在,我面临的问题如下: > 我想要名称空间xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“出现在根元素中,即TokenRestrictionTemplate中,而不是单个子元素中。如何实现这一点? 我有一些元素,例如带有@Xm