Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i remove namespace from the generated JAXB File?

Tags:

jaxb

ant

xsd

xjc

Here is my code:

xsdFile:

<complexType name="Player">
    <sequence>
        <element name="Login" type="string"></element>
        <element name="Passwd" type="string"></element>
    </sequence>
</complexType>

<element name="Player" type="tns:Player"></element>

Build.xml:

    <exec executable="${javahome}/bin/xjc"  >

        <arg value="-extension" />
        <arg value="-b" />
        <arg value="binding.xml" />
        <arg value="-d" />
        <arg value="${sources}" />
        <arg value="-p" />
        <arg value="metier" />
        <arg value="Player.xsd" />

    </exec>
</target>

binding.xml:

<jxb:bindings 
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jxb:extensionBindingPrefixes="xjc" elementFormDefault="qualified" attributeFormDefault="unqualified"
version="2.1">

<jxb:globalBindings>
    <xjc:simple />
    <xjc:serializable/>
</jxb:globalBindings>

And finnaly:

JAXBContext context = JAXBContext.newInstance(Player.class,ObjectFactory.class); Unmarshaller decodeur = context.createUnmarshaller();

i add "xjc:simple" in order to have @XMLRootElement, but an exception is raised: javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.example.org/Player"

It didn't work correctly because i got this:@XmlRootElement(name = "Player", namespace = "http://www.example.org/Player")

Instead of just: @XmlRootElement(name = "Player")

How can i remove this "namespace" ?

Thanks

like image 233
Dupont Avatar asked May 28 '11 14:05

Dupont


1 Answers

If your XML schema indicates that the corresponding XML documents should be namespace qualified, then JAXB will generate a Java model with the expected namespace qualification. Below I'll describe a way in which you could leverage a StAX parser to fool JAXB into thinking it is parsing a namespace qualfied document:

Player

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Player", namespace="http://www.example.org/Player")
public class Player {

    private String login;
    private String passwd;

    @XmlElement(name="Login", namespace="http://www.example.org/Player")
    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    @XmlElement(name="Passwd", namespace="http://www.example.org/Player")
    public String getPasswd() {
        return passwd;
    }

    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

}

NamespaceDelegate

We will create an implementation of StreamReaderDelegate. This delegate will report the namespace for all element events to be "http://www.example.org/Player". Note: This trick assumes that all your elements are qualified with the same namespace URI.

import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.util.StreamReaderDelegate;

public class NamespaceDelegate extends StreamReaderDelegate {

    private static String NAMESPACE = "http://www.example.org/Player";

    public NamespaceDelegate(XMLStreamReader xsr) {
        super(xsr);
    }

    @Override
    public String getNamespaceURI() {
        return NAMESPACE;
    }

}

Demo

import java.io.FileInputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.util.StreamReaderDelegate;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Player.class);

        FileInputStream xmlStream = new FileInputStream("input.xml");
        XMLInputFactory xif = XMLInputFactory.newFactory();
        XMLStreamReader xsr = xif.createXMLStreamReader(xmlStream);
        StreamReaderDelegate srd = new NamespaceDelegate(xsr);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Player player = (Player) unmarshaller.unmarshal(srd);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(player, System.out);
    }

}

input.xml

<?xml version="1.0" encoding="UTF-8"?>
<Player>
    <Login>FOO</Login>
    <Passwd>BAR</Passwd>
</Player>
like image 171
bdoughan Avatar answered Sep 28 '22 05:09

bdoughan