How to unmarshal this type of xml in java -
i new rest api. have xml output need unmarshal. below xml output:
<dsml> <entries> <entry dn="uid=7686,c=in,ou=pages,o=example.com"> <att name="uid"> <value>7568766</value> </att> <att name="email"> <value>new@gmail.com</value> </att> <att name="callname"> <value>john</value> </att> </entry> <entry dn="uid=7689,c=in,ou=pages,o=example.com"> <att name="uid"> <value>7678766</value> </att> <att name="callname"> <value>mike</value> </att> </entry> <entry dn="uid=7690,c=in,ou=pages,o=example.com"> <att name="uid"> <value>75858766</value> </att> <att name="email"> <value>old@gmail.com</value> </att> <att name="callname"> <value>rahul</value> </att> </entry> </entries> </dsml>
the actual xml output has total 37 entries. below model class:
@xmlaccessortype(xmlaccesstype.field) @xmlrootelement(name = "user") public class user implements serializable{ private static final long serialversionuid = 1l; @xmlelement(name = "uid") private int uid; @xmlelement(name = "callname") private string callname; @xmlelement(name = "email") private string email; public int getid() { return uid; } public void setid(int uid) { this.uid = uid; } public string getfirstname() { return callname; } public void setfirstname(string callname) { this.callname = callname; } public string getmail() { return email; } public void setmail(string email) { this.email = email; } }
below rest api class code:
public class usingrestapi { public static void main(string[] args) { try { url url = new url("www.example.com"); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setrequestmethod("get"); conn.setrequestproperty("accept", "application/xml"); if (conn.getresponsecode() != 200) { throw new runtimeexception("failed : http error code : " + conn.getresponsecode()); } bufferedreader br = new bufferedreader(new inputstreamreader((conn.getinputstream()))); string apioutput="",temp=""; while ((apioutput = br.readline()) != null) { temp += apioutput; system.out.println(apioutput); } jaxbcontext jaxbcontext = jaxbcontext.newinstance(user.class); unmarshaller jaxbunmarshaller = jaxbcontext.createunmarshaller(); user user = (user) jaxbunmarshaller.unmarshal(new stringreader(temp)); system.out.println(user.getid()); system.out.println(user.getfirstname()); system.out.println(user.getmail()); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (jaxbexception e) { e.printstacktrace(); } } }
while executing code, getting error
javax.xml.bind.unmarshalexception - linked exception: [org.xml.sax.saxparseexception; linenumber: 1; columnnumber: 110; external dtd: failed read external dtd 'dsml.dtd', because 'http' access not allowed due restriction set accessexternaldtd property.]
i have tried changing propert access_external_dtd true, gave error:
javax.xml.bind.propertyexception: name: http://javax.xml.xmlconstants/property/accessexternaldtd value: true @ javax.xml.bind.helpers.abstractunmarshallerimpl.setproperty(unknown source) @ com.sun.xml.internal.bind.v2.runtime.unmarshaller.unmarshallerimpl.setproperty(unknown source) @ usingrestapi.main(usingrestapi.java:60)
why donot save xml in file , pass unmarshal
new version_:
user user = (user) jaxbunmarshaller.unmarshal(new file("c:/temp/user.xml") );//file path
it not give errors.
visit-: http://howtodoinjava.com/jaxb/jaxb-exmaple-marshalling-and-unmarshalling-list-or-set-of-objects/
Comments
Post a Comment