XML deserialization using jackson-dataformat-xml custom root element? XML deserialization using jackson-dataformat-xml custom root element? json json

XML deserialization using jackson-dataformat-xml custom root element?


Try to configure your XmlMapper to ignore namespaces :

XMLInputFactory input = new WstxInputFactory();input.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);XmlMapper xmlMapper = new XmlMapper(new XmlFactory(input, new WstxOutputFactory()));

Also, you might have to change your @XmlRootElement(name= "student") to @XmlRootElement(name= "prefix:student")

Edit :

As StaxMan said, your problem come from the fact that you use a prefix that has not been declared.

This can be deduced by the log you gave us (Line 1 and line 9 => Undeclared namespace prefix "prefix").

I was under the impression that you could not modify your XML String.

But if you can, StaxMan solution is cleaner and you should add the namespace declaration instead of disabling your parser namespace validation :

<prefix:student xmlns:prefix="http://www.somenamespace.org">  <name>    Jack Jones  </name>  <id>1</id></prefix:student>


Your problem is that XML input is invalid as it is missing the namespace declaration for prefix: if namespace handling is enabled (as it is by most modern XML parsers), Jackson XML module can't do anything about that.

But as per other answer it is often possible to configure underlying XML parser to disable namespace handling, in which case the whole element name is just considered part of "local name", and no namespace information will be maintained.

Just make sure to use either Woodstox or Aalto Stax parsers: one in JDK does not support no-namespace mode, I think.