java - To convert SOAP response to JSONArray -


i have soap response below. want iterate on soap message , want data in listmetadataresponse tag in jsonarray format. here sample soap response:

 <?xml version="1.0" encoding="utf-8"?>     <soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://soap.sforce.com/2006/04/metadata">        <soapenv:body>           <listmetadataresponse>              <result>                 <createdbyid>00528000001m5rraay</createdbyid>                 <createdbyname>hariprasath thanarajah</createdbyname>                 <createddate>1970-01-01t00:00:00.000z</createddate>                 <filename>objects/emailmessage.object</filename>                 <fullname>emailmessage</fullname>                 <id />                 <lastmodifiedbyid>00528000001m5rraay</lastmodifiedbyid>                 <lastmodifiedbyname>hariprasath thanarajah</lastmodifiedbyname>                 <lastmodifieddate>1970-01-01t00:00:00.000z</lastmodifieddate>                 <namespaceprefix />                 <type>customobject</type>              </result>               <result>                 <createdbyid>00528000001m5rraay</createdbyid>                 <createdbyname>hariprasath thanarajah</createdbyname>                 <createddate>1970-01-01t00:00:00.000z</createddate>                 <filename>objects/emailmessage.object</filename>                 <fullname>emailmessage</fullname>                 <id />                 <lastmodifiedbyid>00528000001m5rraay</lastmodifiedbyid>                 <lastmodifiedbyname>hariprasath thanarajah</lastmodifiedbyname>                 <lastmodifieddate>1970-01-01t00:00:00.000z</lastmodifieddate>                 <namespaceprefix />                 <type>customobject</type>              </result>           </listmetadataresponse>        </soapenv:body>     </soapenv:envelope> 

i want each of result nodes jsonobject each attribute node , values key value pair in json.so, in case, want result jsonarray 2 results jsonobject in it.

i have tried code. getting node names not getting node values.

private static document loadxmlstring(string response) throws exception {     documentbuilderfactory dbf = documentbuilderfactory.newinstance();     documentbuilder db = dbf.newdocumentbuilder();     inputsource = new inputsource(new stringreader(response));      return db.parse(is); }  public static jsonarray getfulldata(string tagname, string request) throws exception {     jsonarray resultarray = new jsonarray();     document xmldoc = loadxmlstring(request);     nodelist nodelist = xmldoc.getelementsbytagname("*");     (int = 0; < nodelist.getlength(); i++) {         node node = nodelist.item(i);         if (node.getnodetype() == node.element_node) {             if (node.getnodename().equals("result")) {                 jsonobject rootobject = new jsonobject();                 nodelist childnodelist = nodelist.item(i).getchildnodes();                 (int j = 0; j < childnodelist.getlength(); j++) {                     node = childnodelist.item(i);                     rootobject.put(node.getnodename(), node.getnodevalue());                 }                 resultarray.put(rootobject);             }         }     } } 

you can use json-java library stleary.

you can use following code convert xml string jsonobject.

jsonobject data = xml.tojsonobject(xmlstring);

you can find more info here: json-java


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -