gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:34:18 25 4
gpt4 key购买 nike

我正在使用 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 class is not in the same package. How do I include it in the Jaxb class and generate the xml?

最佳答案

添加:

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

到相应包的package-info.java

确保Money位于同一个包中。

我的架构中还缺少 targetNamespace="http://w.com/"

关于java - 生成的 XML 中的子元素没有父级的命名空间 - JAXB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49659631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com