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

为君一笑 提交于 2020-05-31 04:21:33

问题


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


回答1:


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.




回答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):

  • http://blog.bdoughan.com/2011/11/jaxb-and-namespace-prefixes.html


来源:https://stackoverflow.com/questions/4040682/jaxb-in-java-6-not-prefixing-the-correct-namespace-prefix-in-marshalled-xml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!