如何使用JAXB编组消除元素/属性中的自动生成的命名空间前缀

时间:2011-07-30 01:27:46

标签: java xml jaxb marshalling

问题

如何消除使用JAXB编组时出现在所有元素和属性上的自动生成的命名空间前缀

我在编组和预期输出后展示了我当前的XML输出。

详情

我正在使用随JDK 1.6 update 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);

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>

当前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>

所需的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>

1 个答案:

答案 0 :(得分:3)

您可以使用NamespacePrefixMapper

marshallerDXD.setProperty("com.sun.xml.bind.namespacePrefixMapper",
                          myNsPrefixMapper);

控制名称空间前缀:

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];
  }
}

我用以下方法测试了编组:

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); 

和这个JAXB实现:

<dependency>
  <groupId>com.sun.xml.bind</groupId>
  <artifactId>jaxb-impl</artifactId>
  <version>2.2.2</version>
  <type>jar</type>
  <scope>compile</scope>
</dependency>
相关问题