Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

So I am using Jackson for JSON serialization/deserialization succesfully uptil now, now I am also trying to use it for XML serialization/deserialization using it's jackson-dataformat-xml-2.3.0.jar.

I have used

objectmapper.reader(Student.class).withRootName("prefix:student").readValue(jsonString)

to deserialize my JSON (having root name with prefix) to Student class which worked succesfully.

Now, I am trying to deserialize my XML string (having root name with prefix) to Student :

XML:

<prefix:student>
  <name>
    Jack Jones
  </name>
  <id>1</id>
</prefix:student>

My POJO:

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "name",
        "id",
        ....
    })
    @XmlRootElement(name = "student")
    public class Student
    {

        protected String name;
        BigInteger id;  
..........................
    }

I was desrializing this using XmlMapper in jackson-dataformat-xml like this:

xmlMapper.reader(Student.class).withRootName("prefix:student").readValue(xmlString)

I get the following exception:

java.io.IOException: com.ctc.wstx.exc.WstxParsingException: Undeclared namespace prefix "prefix"
 at [row,col {unknown-source}]: [1,9]
    at com.fasterxml.jackson.dataformat.xml.util.StaxUtil.throwXmlAsIOException(StaxUtil.java:24)
    at com.fasterxml.jackson.dataformat.xml.XmlFactory._createParser(XmlFactory.java:473)
    at com.fasterxml.jackson.dataformat.xml.XmlFactory._createParser(XmlFactory.java:26)
    at com.fasterxml.jackson.core.JsonFactory.createParser(JsonFactory.java:844)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2091)

Caused by: com.ctc.wstx.exc.WstxParsingException: Undeclared namespace prefix "prefix"
 at [row,col {unknown-source}]: [1,9]
    at com.ctc.wstx.sr.StreamScanner.constructWfcException(StreamScanner.java:606)
    at com.ctc.wstx.sr.StreamScanner.throwParseError(StreamScanner.java:479)
    at com.ctc.wstx.sr.InputElementStack.resolveAndValidateElement(InputElementStack.java:507)
    at com.ctc.wstx.sr.BasicStreamReader.handleStartElem(BasicStreamReader.java:2977)
    at com.ctc.wstx.sr.BasicStreamReader.handleRootElem(BasicStreamReader.java:2098)
    at com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2078)
    at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1134)
    at com.fasterxml.jackson.dataformat.xml.XmlFactory._initializeXmlReader(XmlFactory.java:581)
    at com.fasterxml.jackson.dataformat.xml.XmlFactory._createParser(XmlFactory.java:471)
    ... 6 more

What do I need to do for XML deserialization with custom root name ??

like image 878
Siddharth Trikha Avatar asked Sep 02 '16 11:09

Siddharth Trikha


1 Answers

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>
like image 121
Dimpre Jean-Sébastien Avatar answered Oct 30 '22 04:10

Dimpre Jean-Sébastien