首页 文章

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

提问于
浏览
1

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

我不确定我在做什么错 . 下面是我的xsd和xml以及类 .

XSD:

ItemResponses.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类

ItemResponses.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.
}

ItemResponse.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>

package-info.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;

Money类不在同一个包中 . 如何将其包含在Jaxb类中并生成xml?

1 回答

  • 0

    加:

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

    到相应包的 package-info.java .

    确保 Money 在同一个包中 .

    我也错过了架构中的 targetNamespace="http://w.com/" .

相关问题