function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Stephen E 4Stephen E 4 

Parsing XML using Dom on more complex xml?

I am currently trying to parse an XML response, however it does not seem to want to parse whats being returned, and im not sure if its because of the complexity of the response, or if im doing something wrong.

I am using HttpRequests to send requets to our api. 

Here is the xml the API responds with:
 
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <getGLBranchResponse xmlns="our end point is here">
      <getGLBranchResult>
        <Branch>
          <ID>
            <Value>int</Value>
            <Valid>boolean</Valid>
          </ID>
          <Code>
            <Value>string</Value>
            <Valid>boolean</Valid>
          </Code>
        </Branch>
        <BranchName>
          <Value>string</Value>
          <Valid>boolean</Valid>
        </BranchName>
        <Customer>
          <ID>
            <Value>int</Value>
            <Valid>boolean</Valid>
          </ID>
          <Code>
            <Value>string</Value>
            <Valid>boolean</Valid>
          </Code>
        </Customer>
        <DefaultWarehouse>
          <ID>
            <Value>int</Value>
            <Valid>boolean</Valid>
          </ID>
          <Code>
            <Value>string</Value>
            <Valid>boolean</Valid>
          </Code>
        </DefaultWarehouse>
        <MainBranch>
          <Value>boolean</Value>
          <Valid>boolean</Valid>
        </MainBranch>
        <Active>
          <Value>boolean</Value>
          <Valid>boolean</Valid>
        </Active>
      </getGLBranchResult>
    </getGLBranchResponse>
  </soap:Body>
</soap:Envelope>

I use Dom.Document doc = res.getBodyDocument(); to get the document from the HTTP response. 

When I call Dom.XMLNode rootele = doc.getRootElement(); and put it in a system.debug, it doesnt really make much sense, returns something like:
 
[13]|DEBUG|XMLNode[ELEMENT,Envelope,http://schemas.xmlsoap.org/soap/envelope/,null,[common.apex.api.dom.XmlNode$NamespaceDef@c2d03a7, common.apex.api.dom.XmlNode$NamespaceDef@59ff7f61, common.apex.api.dom.XmlNode$NamespaceDef@32f5bbfc],[XMLNode[ELEMENT,Body,http://schemas.xmlsoap.org/soap/envelope/,null,null, etc

when I call getChildElement on this, it returns null, everytime. I have tried calling getChildElement('Envelope',null)​, getChildElement('Body',null)​ and many others. Always returns null. Am i doing something wrong? 
Best Answer chosen by Stephen E 4
Alain CabonAlain Cabon
Hi Stephen,

You have a common problem of namespaces (it is not "null" everywhere here).

<soap:Body> depends on the namespace xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"

If you don't precise the namespace, you can get the values depending of Body.

I often parse XML with Xquery requests and the namespaces are very important but the defaut examples in the documentation never use them.

getChildElement(name, namespace) : Returns the child element node for the node with the given name and namespace.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_xml_dom_xmlnode.htm

The complete solution here because there is a problem in the problem for this namespace sometimes:
https://developer.salesforce.com/forums/?id=906F0000000AbNMIA0 

Regards
Alain

All Answers

Alain CabonAlain Cabon
Hi Stephen,

You have a common problem of namespaces (it is not "null" everywhere here).

<soap:Body> depends on the namespace xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"

If you don't precise the namespace, you can get the values depending of Body.

I often parse XML with Xquery requests and the namespaces are very important but the defaut examples in the documentation never use them.

getChildElement(name, namespace) : Returns the child element node for the node with the given name and namespace.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_xml_dom_xmlnode.htm

The complete solution here because there is a problem in the problem for this namespace sometimes:
https://developer.salesforce.com/forums/?id=906F0000000AbNMIA0 

Regards
Alain
This was selected as the best answer
Alain CabonAlain Cabon
If you don't precise the namespace, you cannot get the values depending of Body (I am a little tired) but if your problem is solved that is a good point.
Regards.
Harsh Singh 7Harsh Singh 7
{
  "Envelope": {
    "Body": {
      "getGLBranchResponse": {
        "getGLBranchResult": {
          "Branch": {
            "ID": {
              "Value": "int",
              "Valid": "boolean"
            },
            "Code": {
              "Value": "string",
              "Valid": "boolean"
            }
          },
          "BranchName": {
            "Value": "string",
            "Valid": "boolean"
          },
          "Customer": {
            "ID": {
              "Value": "int",
              "Valid": "boolean"
            },
            "Code": {
              "Value": "string",
              "Valid": "boolean"
            }
          },
          "DefaultWarehouse": {
            "ID": {
              "Value": "int",
              "Valid": "boolean"
            },
            "Code": {
              "Value": "string",
              "Valid": "boolean"
            }
          },
          "MainBranch": {
            "Value": "boolean",
            "Valid": "boolean"
          },
          "Active": {
            "Value": "boolean",
            "Valid": "boolean"
          }
        }
      }
    }
  }
}
if you received the solution that you expected then select the best answer