java - cannot convert xml to jaxb -


i have problem converting xml java class, cannot see problem.

public myresponse getsmartfocusresponse(myresponse myresponse, string data) throws jaxbexception {     try {         jaxbcontext jaxbcontext = jaxbcontext.newinstance(myresponse.getclass());         unmarshaller unmarshaller = jaxbcontext.createunmarshaller();          stringreader reader = new stringreader(data);         myresponse response = (myresponse) unmarshaller.unmarshal(reader);          return response;     } catch (jaxbexception e) {         e.printstacktrace();         throw e;     } } 

here class convert to:

@xmlrootelement(name = "response") public class myresult {  private innerresult result; private string responsestatus;  @xmlelement(name = "result") public void setresult(innerresult uploadstatus) {     this.result = uploadstatus; }  @xmlattribute(name = "responsestatus") public void setresponsestatus(string responsestatus) {     this.responsestatus = responsestatus; }  public innerresult getresult() {     return result; }  public string getresponsestatus() {     return responsestatus; }  }   @xmlrootelement(name = "result") public class innerresult {  private string content; private string attribute;  @xmlattribute(name = "xsi:type") public void setattribute(string attribute) {     this.attribute = attribute; }  @xmlelement public void setcontent(string content) {     this.content = content; }  public string getattribute() {     return attribute; }  public string getcontent() {     return content; }  } 

finally, here xml try convert:

<response responsestatus="success">     <result xsi:type="xs:string">         connection closed     </result> </response> 

the exception catch is:

javax.xml.bind.unmarshalexception - linked exception: [org.xml.sax.saxparseexception; linenumber: 1; columnnumber: 120; prefix "xsi" attribute "xsi:type" associated element type "result" not bound.]

the problem

the following xml document not valid namespace aware parser expect there namespace associated prefix "xsi":

<response responsestatus="success">     <result xsi:type="xs:string">         connection closed     </result> </response> 

below xml parser wants:

<response responsestatus="success" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">     <result xsi:type="xs:string">         connection closed     </result> </response> 

the fix

i cannot change xml, input 3d party...

since cannot change xml need use xml parser not namespace aware. news there 1 right in jdk/jre.

import javax.xml.bind.jaxbcontext; import javax.xml.bind.marshaller; import javax.xml.bind.unmarshaller; import javax.xml.bind.unmarshallerhandler; import javax.xml.parsers.saxparser; import javax.xml.parsers.saxparserfactory;  import org.xml.sax.inputsource; import org.xml.sax.xmlreader;  public class demo {      public static void main(string[] args) throws exception {         saxparserfactory spf = saxparserfactory.newinstance();         saxparser sp = spf.newsaxparser();         xmlreader xr = sp.getxmlreader();          jaxbcontext jc = jaxbcontext.newinstance(myresponse.class);         unmarshaller unmarshaller = jc.createunmarshaller();         unmarshallerhandler unmarshallerhandler = unmarshaller.getunmarshallerhandler();         xr.setcontenthandler(unmarshallerhandler);          string data = ...;         stringreader reader = new stringreader(data);         inputsource xmlsource = new inputsource(reader);         xr.parse(xmlsource);          myresponse myresponse = (myresponse) unmarshallerhandler.getresult();      }  } 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -