You need to sign in to do that
Don't have an account?
Stephen 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:
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:
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?
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?
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
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
Regards.
Regards.
"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