views:

24

answers:

2

Hi

I have a schema with following attributes in schema element:

<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:abc="http://abc.example.com" targetNamespace="http://abc.example.com" elementFormDefault="qualified" attributeFormDefault="unqualified">

I could compile it and get java classes. Using these classes, I filled in data in an object and marshalled it to get an XML file.

But the XML elements in this marshalled file is not having the prefix "abc".

I tweaked a little using XMLStreamWriter.setPrefix while marshalling, and I could get the XML file with all elements prefixed as "abc".

Now when I try to unmarshall the same XML file, it throws error saying :

[org.xml.sax.SAXParseException: The prefix "abc" for element "abc:Name" is not bound.]

Please help me in how to let JAXB know that abc is correct prefix.

Thank You Pradeep

A: 

The name of the prefix is meaningless. All it does is make a connection between a namespace and the tags that belong to that namespace. Whether the prefix is abc or namespace01 or there is no prefix because the default namespace is used doesn't matter. As far as I know you can't force the usage of a prefix or the specific name of a prefix in JAXB.

musiKk
thanks musiKk, now I understand it.But if we want to use our own defined prefix, we can use JAXB RI which contains NamespacePrefixMapper along with JAXB from Java 6. [include JAXB RI in classpath].marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespaceMapper());
pradeep
A: 

Check out my answer to a previous question to see how this can be done with MOXy JAXB:

Blaise Doughan