How can i remove namespace from the generated JAXB File?
-
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
-
Answer:
I just delete "ObjectFactory.class" and it works. New code: JAXBContext context = JAXBContext.newInstance(Player.class); Unmarshaller decodeur = context.createUnmarshaller();
Dupont at Stack Overflow Visit the source
Other 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 http://download.oracle.com/javase/6/docs/api/javax/xml/stream/util/StreamReaderDelegate.html. 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>
Blaise Doughan
Related Q & A:
- how can i remove an array from an array?Best solution by Stack Overflow
- How can I remove duplicate Objects from array of Objects in javascript?Best solution by Stack Overflow
- How can I remove an element in a repeated list?Best solution by Stack Overflow
- How can I remove hyperlink?Best solution by Yahoo! Answers
- How can i export my contacts to a csv file?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.