java - Jackson XML - deserializing XML with namespace prefixes -
i'm working jackson xml plugin (https://github.com/fasterxml/jackson-dataformat-xml), , i'm not sure if it's supported, i'm wondering if it's possible both serialize , deserialize xml namespace prefixes, so:
<name:foo> <name:bar> <name:x>x</name:x> <name:y>y</name:y> </name:bar> </name:foo>
i can generate type of xml using jackson's plugin so:
@jacksonxmlproperty(localname="name:bar") public bar getbar() { return bar; }
however, can't find way configure pojos deserialize xml generated. please see following example code:
public class bar{ @jacksonxmlproperty(localname="name:x") public string x = "x"; @jacksonxmlproperty(localname="name:y") public string y = "y"; }
@jacksonxmlrootelement(localname="name:foo") public class foo{ private bar bar; @jacksonxmlproperty(localname="name:bar") public bar getbar() { return bar; } public void setbar(bar bar) { this.bar = bar; } }
public class testdeserialization { public static void main(string[] args) throws exception { foo foo = new foo(); foo.setbar(new bar()); xmlmapper xmlmapper = new xmlmapper(); string xml = xmlmapper.writerwithdefaultprettyprinter().writevalueasstring(foo); system.out.println(xml); system.out.println("xml desearialzing...."); foo foo2= xmlmapper.readvalue(xml, foo.class); system.out.println(xmlmapper.writevalueasstring(foo2)); } }
trying run test gives me exception:
exception in thread "main" java.io.ioexception: com.ctc.wstx.exc.wstxparsingexception: undeclared namespace prefix "name"
which understandable, wondering if there's way work jackson xml?
jacksonxmlproperty
annotation has property namespace
. use defining namespace
public class bar { @jacksonxmlproperty(namespace = "name",localname="x") public string x = "x"; @jacksonxmlproperty(namespace = "name",localname="y") public string y = "y"; } @jacksonxmlrootelement(namespace = "name", localname = "foo") public class foo { private bar bar; @jacksonxmlproperty(namespace = "name", localname = "bar") public bar getbar() { return bar; } public void setbar(bar bar) { this.bar = bar; } }
Comments
Post a Comment