JAXB in java 6 not prefixing the correct namespace prefix in marshalled XML file
Asked Answered
O

2

2

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

Optimal answered 28/10, 2010 at 7:26 Comment(0)
C
2

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.

Candice answered 28/10, 2010 at 7:51 Comment(3)
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());Optimal
@pradeep: Nice. Even though you make yourself dependent on a specific implementation it's good to know.Candice
Don't totally agree on the "meaningless" part. It may be "meaningless" from a technical point of view. But assume that e.g. - in any way - a human will see or work with the XML, it might be important to be able to set the namespace prefixes in a meaningful way...Acetate
P
2

In the article below I demonstrate how this can be achieved using NamespacePrefixMapper from the JAXB RI, and the @XmlSchema annotation with EclipseLink JAXB (MOXy):

Precise answered 28/10, 2010 at 9:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.