开发者

JAXB Marshalling and unmarshalling avoiding namespaces

开发者 https://www.devze.com 2023-01-09 23:00 出处:网络
this is a very common problem but I still need a specific solution if someone can please provide me with a solution.

this is a very common problem but I still need a specific solution if someone can please provide me with a solution.

JAXB.unmarshal('开发者_如何学运维file', class);

However because of the namespaces present in the xml file the class object is not getting populated with the inner elements.

The turnaround I found was to create a JAXBContext object create and unmarshaller and then make it namespace aware.

I am inquisitive if there are any other solutions to the same.


If the XML is using namespaces, those are part of the element name and they should not be parsed as if they were unnamespaced elements. Therefore that is not an option in JAXB. A previous question along these lines have given the only solution as sanitizing the XML prior to unmarshalling and removing all namespaces, with the JAXB objects bound to unnamespaced names.


If the nodes are always qualified with namespaces URIs the you should add this namespace information you your JAXB bindings. Many JAXB annotations take a namespace parameter (I.e XmlRootElement, XmlAttribute, XmlElement, etc.). Also there is a package level annotation called @XmlSchema you can use you to specify a default namespace and the default namespace qualification rules.

0

精彩评论

暂无评论...
验证码 换一张
取 消