Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to eliminate auto-generated namespace prefix in elements/attributes using JAXB marshalling

Question

How to eliminate auto-generated namespace prefix that appears on all elements and attributes when using JAXB marshalling

I've demonstrated my current XML output after marshalling and the expected output.

Details

I'm using the default JaxB implementation (Metro) provided with JDK 1.6 update 21.

My XSD file is shown below. I used xjc to generate the Java Classes for this XSD and I dont want to add/change any annotations in the generated Java classes, so that I can continue to use xjc.

In the code, this is how I marshal....where I create MYJAVAOBJECTTREE using ObjectFactory etc..

    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 File

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

Current XML File Output

    <?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>

Desired XML File Output is

    <?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>
like image 865
FatherFigure Avatar asked Oct 24 '22 04:10

FatherFigure


1 Answers

You can use a NamespacePrefixMapper

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

to have control for the namespace prefixes:

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

I've tested the marshalling with:

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

and this JAXB implementation:

<dependency>
  <groupId>com.sun.xml.bind</groupId>
  <artifactId>jaxb-impl</artifactId>
  <version>2.2.2</version>
  <type>jar</type>
  <scope>compile</scope>
</dependency>
like image 162
Thor Avatar answered Nov 08 '22 07:11

Thor