![](/img/trans.png)
[英]How to remove xmlns:xsi and xsi:type from JAXB marshalled XML file
[英]JAXB: Concrete type information (xsi:type) missing on marshalled XML
我正在為XSD生成JAXB類,用於SOAP WebService我正在構建一個客戶端(使用jaxws-maven-plugin v2.4.1生成,wsimport目標)。
我遇到一個問題,在編組我的對象時,JAXB不會將xsi:type-Information添加到抽象類型的節點。 WebService現在(理所當然,我認為)抱怨我試圖傳遞它的元素而不指定它們是什么類型(“類型定義不能為元素抽象...”)。
這是一個演示我的問題的簡化示例:
抽象類型模式: (abstract.xsd)
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://www.example.com/namespace_abstract"
elementFormDefault="qualified"
attributeFormDefault="qualified"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.example.com/namespace_abstract">
<xsd:complexType name="ElementType" abstract="true">
<xsd:simpleContent>
<xsd:extension base="xsd:string"/>
</xsd:simpleContent>
</xsd:complexType>
</xsd:schema>
具體類型架構: (concrete.xsd)
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://www.example.com/namespace_concrete"
elementFormDefault="qualified"
attributeFormDefault="qualified"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.example.com/namespace_concrete"
xmlns:abstr="http://www.example.com/namespace_abstract">
<xsd:import namespace="http://www.example.com/namespace_abstract" schemaLocation="abstract.xsd"/>
<!-- Concrete type -->
<xsd:complexType name="ElementTypeExtension">
<xsd:simpleContent>
<xsd:restriction base="abstr:ElementType">
<xsd:enumeration value="one"/>
<xsd:enumeration value="two"/>
<xsd:enumeration value="three"/>
</xsd:restriction>
</xsd:simpleContent>
</xsd:complexType>
<!-- Type that has a field of the abstract type -->
<xsd:complexType name="ExtensionTypeContainer">
<xsd:sequence>
<xsd:element name="type" type="abstr:ElementType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
測試:
import com.example.namespace_concrete.*;
import javax.xml.bind.*;
import javax.xml.bind.annotation.XmlRootElement;
public class MarshallingTest {
public static void main(String[] args) throws JAXBException {
ElementTypeExtension elementTypeExtension = new ElementTypeExtension();
elementTypeExtension.setValue("one");
ExtensionTypeContainer extensionTypeContainer = new ExtensionTypeContainer();
extensionTypeContainer.setType(elementTypeExtension);
XmlRoot xmlRoot = new XmlRoot();
xmlRoot.setContainer(extensionTypeContainer);
Marshaller marshaller = JAXBContext.newInstance(XmlRoot.class, ExtensionTypeContainer.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(xmlRoot, System.out);
}
@XmlRootElement
static class XmlRoot {
private ExtensionTypeContainer container;
public ExtensionTypeContainer getContainer() { return container; }
public void setContainer(ExtensionTypeContainer container) { this.container = container; }
}
}
輸出:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xmlRoot xmlns:ns2="http://www.example.com/namespace_concrete">
<container>
<ns2:type>one</ns2:type>
</container>
</xmlRoot>
缺少的部分是ns2:type
節點上的xsi:type="ns2:ElementTypeExtension"
,使得類型定義不明確(當然在我的示例中只有一種具體類型,但仍然如此)。
我找到了一種方法來生成xsi:type
,方法是修改生成的抽象類源代碼(為了便於閱讀,刪除了JAXB-javadoc):
package com.example.namespace_abstract;
import javax.xml.bind.annotation.*;
import com.example.namespace_concrete.ElementTypeExtension;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ElementType", propOrder = { "value" })
@XmlSeeAlso({ ElementTypeExtension.class })
public abstract class ElementType {
@XmlValue
protected String value;
public String getValue() { return value; }
public void setValue(String value) { this.value = value; }
}
一旦我從value
字段中刪除@XmlValue
-Annotation, xsi:type
-Information就會出現在編組的XML中。 源代碼是從XSD生成的,所以這不是一個真正的選擇。
任何人都可以給我一個關於如何獲得xsi:type
的想法嗎? 修改xsd將是一個選項。
更新到JAXB 2.2.11修復了這個問題,似乎與這個JAXB問題有關 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.