java - How do i unmarshall a xml-document element into object property? -


i want unmarshall element class property, type object, keep generic.

i tried build class , marshall xml, unmarshall back,the result fine. when try normal generated xml-document(although has same structure), value of result class object property null.

here teststructure:

@xmlrootelement public class teststructure {      private object test;      public object gettest() {         return test;     }      public void settest(object test) {         this.test = test;     } } 

i try marshall this, , xml-document:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <teststructure> <test xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xs="http://www.w3.org/2001/xmlschema" xsi:type="xs:string">foo</test> </teststructure> 

however, if try arbitrarly build structure via domsource same xml document:

documentbuilderfactory docfactory = documentbuilderfactory.newinstance(); documentbuilder docbuilder = docfactory.newdocumentbuilder();  // root element document doc = docbuilder.newdocument(); element rootelement = doc.createelement("teststructure"); doc.appendchild(rootelement); element test = doc.createelement("test"); test.appendchild(doc.createtextnode("foo")); test.setattribute("xmlns:xsi", "http://www.w3.org/2001/xmlschema-instance"); test.setattribute("xsi:type", "xs:string"); test.setattribute("xmlns:xs", "http://www.w3.org/2001/xmlschema"); rootelement.appendchild(test); 

and unmarshal document:

jaxbcontext context2 = jaxbcontext.newinstance(teststructure.class); unmarshaller m2 = context2.createunmarshaller();  teststructure testobject2 =   ( teststructure ) m2.unmarshal(doc);  system.out.println(testobject2.gettest()); 

the attribute appears "null".

so, went wrong?

woah, w a , got solution :)

you have modify element attributes:

    element test = doc.createelement("test"); test.appendchild(doc.createtextnode("foo")); test.setattributens("http://www.w3.org/2001/xmlschema-instance", "xsi:type", "xs:string"); test.setattribute("xmlns:xs", "http://www.w3.org/2001/xmlschema"); 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -