我需要像这样生成我的XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<advice_request xmlns="urn:xyz.com:bf:api:core:v1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<instructions>
<instruction i:type="advice_instruction_adjust">
<method>not_set</method>
<quantity>1</quantity>
<site_guid>abcd123</site_guid>
</instruction>
</instructions>
</advice_request>
注意&#34;类型&#34;属性用&#34; i&#34;。
限定我已将xsd定义为:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="urn:xyz.com:bf:api:core:v1"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
targetNamespace="urn:xyz.com:bf:api:core:v1"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:element name="advice_request">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="instructions" type="Instructions" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="Instructions">
<xsd:sequence>
<xsd:element name="instruction" type="Instruction" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Instruction">
<xsd:sequence>
<xsd:element name="method" type="xsd:string" minOccurs="1" maxOccurs="1"/>
<xsd:element name="quantity" type="xsd:int" minOccurs="1" maxOccurs="1"/>
<xsd:element name="site_guid" type="xsd:string" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="type" id="type" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>
XJC生成以下package-info.java:
@XmlSchema(namespace = "urn:xyz.com:bf:api:core:v1", elementFormDefault = QUALIFIED)
然而,在编组对象时,我总是将XML视为:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<advice_request xmlns="urn:xyz.com:bf:api:core:v1">
<instructions>
<instruction type="advice_instruction_adjust">
<method>not_set</method>
<quantity>1</quantity>
<site_guid>abcd123</site_guid>
</instruction>
</instructions>
</advice_request>
如何使用&#34; i&#34;?任何帮助都非常感激。
我尝试了多种方法,例如导入XMLSchema-instance命名空间,明确地在marshaller上设置SCHEMA_LOCATION(尽管这可能不是我的选项)但仍然没有...
提前致谢!
答案 0 :(得分:0)
您应该为complexType
使用层次结构。 i:type="advice_instruction_adjust"
表示有complextType
名为advice_instruction_adjust
。
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="urn:xyz.com:bf:api:core:v1"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
targetNamespace="urn:xyz.com:bf:api:core:v1"
elementFormDefault="qualified" attributeFormDefault="qualified">
<xsd:element name="advice_instruction_adjust" type="advice_instruction_adjust"/>
<xsd:complexType name="advice_instruction_adjust">
<xsd:complexContent>
<xsd:extension base="Instruction">
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="Instructions" type="Instructions"/>
<xsd:complexType name="Instructions">
<xsd:sequence>
<xsd:element name="instruction" type="Instruction" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Instruction" abstract="true">
<xsd:sequence>
<xsd:element name="method" type="xsd:string" minOccurs="1" maxOccurs="1"/>
<xsd:element name="quantity" type="xsd:int" minOccurs="1" maxOccurs="1"/>
<xsd:element name="site_guid" type="xsd:string" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
这是XML的一个例子
<?xml version="1.0" encoding="UTF-8"?>
<ns1:Instructions xmlns:ns1="urn:xyz.com:bf:api:core:v1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ns1:instruction i:type="advice_instruction_adjust">
<ns1:method />
<ns1:quantity/>
<ns1:site_guid/>
</ns1:instruction>
</ns1:Instructions>