Fat*_*ure 5 java xml jaxb marshalling
题
使用 JAXB 编组时如何消除出现在所有元素和属性上的自动生成的命名空间前缀
我已经在编组和预期输出后演示了我当前的 XML 输出。
细节
我正在使用 JDK 1.6 更新 21 提供的默认 JaxB 实现(Metro)。
我的 XSD 文件如下所示。我使用 xjc 为这个 XSD 生成 Java 类,我不想在生成的 Java 类中添加/更改任何注释,以便我可以继续使用 xjc。
在代码中,这就是我编组的方式......在那里我使用 ObjectFactory 等创建 MYJAVAOBJECTTREE ......
JAXBContext jcDXD = JAXBContext.newInstance(MDASJ.class);
QName qn=new QName(XMLDataFormat.XML_ROOT_NAME);
marshallerDXD = jcDXD.createMarshaller();
marshallerDXD.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshallerDXD.setProperty(Marshaller.JAXB_ENCODING, "ISO-8859-1");
marshallerDXD.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.theronyx.com/mdasj/xmldata mdasj-data.xsd");
jaxbElementDXD = new JAXBElement<MDASJ>(qn, MDASJ.class, MYJAVAOBJECTTREE);
marshallerDXD.marshal(jaxbElementDXD, System.out);
Run Code Online (Sandbox Code Playgroud)
XSD 文件
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.theronyx.com/mdasj/xmldata" xmlns="http://www.theronyx.com/mdasj/xmldata">
<!-- definition of attributes -->
<xs:attribute name="ID" type="xs:string"/>
<xs:attribute name="ComputerTime" type="xs:string"/>
<xs:attribute name="VarId" type="xs:string"/>
<xs:attribute name="Value" type="xs:string"/>
<xs:attribute name="DataType" type="xs:string"/>
<!-- definition of complex elements -->
<!-- DIH -->
<xs:element name="DIH">
<xs:complexType>
<xs:attribute ref="ID" use="required"/>
</xs:complexType>
</xs:element>
<!-- TimeStamp -->
<xs:element name="TimeStamp">
<xs:complexType>
<xs:attribute ref="ComputerTime" use="required"/>
</xs:complexType>
</xs:element>
<!-- Variable -->
<xs:element name="Variable">
<xs:complexType>
<xs:attribute ref="VarId" use="required"/>
<xs:attribute ref="Value" use="required"/>
<xs:attribute ref="DataType" />
</xs:complexType>
</xs:element>
<!-- Root Data Spec -->
<xs:element name="MDASJ">
<xs:complexType>
<xs:sequence>
<xs:element ref="DIH"/>
<xs:element ref="TimeStamp"/>
<xs:element ref="Variable" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute ref="ID" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
当前 XML 文件输出
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<MDASJ ns1:ID="MDASJID" xsi:schemaLocation="http://www.theronyx.com/mdasj/xmldata mdasj-data.xsd" xmlns:ns1="http://www.theronyx.com/mdasj/xmldata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns1:DIH ns1:ID="servo1"/>
<ns1:Variable ns1:DataType="Numeric" ns1:Value="0.19830813342577691127388561653788201510906219482421875" ns1:VarId="key1"/>
<ns1:Variable ns1:DataType="Text" ns1:Value="-3815206174054821329" ns1:VarId="key2"/>
</MDASJ>
Run Code Online (Sandbox Code Playgroud)
所需的 XML 文件输出是
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<MDASJ ID="MDASJID" xsi:schemaLocation="http://www.theronyx.com/mdasj/xmldata mdasj-data.xsd" xmlns="http://www.theronyx.com/mdasj/xmldata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<DIH ID="servo1"/>
<Variable DataType="Numeric" Value="0.19830813342577691127388561653788201510906219482421875" VarId="key1"/>
<Variable DataType="Text" Value="-3815206174054821329" VarId="key2"/>
</MDASJ>
Run Code Online (Sandbox Code Playgroud)
您可以使用NamespacePrefixMapper
marshallerDXD.setProperty("com.sun.xml.bind.namespacePrefixMapper",
myNsPrefixMapper);
Run Code Online (Sandbox Code Playgroud)
控制命名空间前缀:
public class MyNsPrefixMapper extends NamespacePrefixMapper
{
public String getPreferredPrefix(String uri, String suggest, boolean require)
{
if("http://www.theronyx.com/mdasj/xmldata".equals(uri) ){return "";}
return suggest;
}
public String[] getPreDeclaredNamespaceUris()
{
// String[] result = new String[1];
// result[0] = "http://www.theronyx.com/mdasj/xmldata";
return new String[0];
}
}
Run Code Online (Sandbox Code Playgroud)
我已经测试了编组:
MDASJ xml = ....;
JAXBContext context = JAXBContext.newInstance(MDASJ.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.setProperty(Marshaller.JAXB_ENCODING, "ISO-8859-1");
m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
"http://www.theronyx.com/mdasj/xmldata mdasj-data.xsd");
m.setProperty("com.sun.xml.bind.namespacePrefixMapper",new MyPrefixMapper());
m.marshal(xml, System.out);
Run Code Online (Sandbox Code Playgroud)
以及这个 JAXB 实现:
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9057 次 |
最近记录: |