title: To convert SOAP response to JSONArray
body: <p>I have the SOAP response as below. I want to iterate over the soap message and want to get the data in the listMetadataResponse tag in JSONArray format. Here is my sample SOAP response:</p>
  
  <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://soap.sforce.com/2006/04/metadata"&gt;
         &lt;soapenv:Body&gt;
            &lt;listMetadataResponse&gt;
               &lt;result&gt;
                  &lt;createdById&gt;00528000001m5RRAAY&lt;/createdById&gt;
                  &lt;createdByName&gt;Hariprasath Thanarajah&lt;/createdByName&gt;
                  &lt;createdDate&gt;1970-01-01T00:00:00.000Z&lt;/createdDate&gt;
                  &lt;fileName&gt;objects/EmailMessage.object&lt;/fileName&gt;
                  &lt;fullName&gt;EmailMessage&lt;/fullName&gt;
                  &lt;id /&gt;
                  &lt;lastModifiedById&gt;00528000001m5RRAAY&lt;/lastModifiedById&gt;
                  &lt;lastModifiedByName&gt;Hariprasath Thanarajah&lt;/lastModifiedByName&gt;
                  &lt;lastModifiedDate&gt;1970-01-01T00:00:00.000Z&lt;/lastModifiedDate&gt;
                  &lt;namespacePrefix /&gt;
                  &lt;type&gt;CustomObject&lt;/type&gt;
               &lt;/result&gt;
                &lt;result&gt;
                  &lt;createdById&gt;00528000001m5RRAAY&lt;/createdById&gt;
                  &lt;createdByName&gt;Hariprasath Thanarajah&lt;/createdByName&gt;
                  &lt;createdDate&gt;1970-01-01T00:00:00.000Z&lt;/createdDate&gt;
                  &lt;fileName&gt;objects/EmailMessage.object&lt;/fileName&gt;
                  &lt;fullName&gt;EmailMessage&lt;/fullName&gt;
                  &lt;id /&gt;
                  &lt;lastModifiedById&gt;00528000001m5RRAAY&lt;/lastModifiedById&gt;
                  &lt;lastModifiedByName&gt;Hariprasath Thanarajah&lt;/lastModifiedByName&gt;
                  &lt;lastModifiedDate&gt;1970-01-01T00:00:00.000Z&lt;/lastModifiedDate&gt;
                  &lt;namespacePrefix /&gt;
                  &lt;type&gt;CustomObject&lt;/type&gt;
               &lt;/result&gt;
            &lt;/listMetadataResponse&gt;
         &lt;/soapenv:Body&gt;
      &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>I want to get each of the result nodes as JSONObject with each attribute node and values as key value pair in JSON.So, in this case, I want the result as a JSONArray with two results JSONObject in it.</p>
  
  <p>I have tried this code. I am getting node names but I am not getting the node values.</p>
  
  <pre><code>private static Document loadXMLString(String response) throws Exception {
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      InputSource is = 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("*");
      for (int i = 0; i &lt; 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();
                  for (int j = 0; j &lt; childNodeList.getLength(); j++) {
                      node = childNodeList.item(i);
                      rootObject.put(node.getNodeName(), node.getNodeValue());
                  }
                  resultArray.put(rootObject);
              }
          }
      }
  }
  </code></pre>