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
Frederik BootFrederik Boot 

DOM Document - multi level XML

Hello,

I have the following XML:
 
<OrderDetails>
<OrderDetailID>2584</OrderDetailID>
<ProductCode>AS-456</ProductCode>
<GiftWrap>
<GiftWrapCost>0.0000</GiftWrapCost>
<GiftWrapType>Paper</GiftWrapType>
</GiftWrap>
</OrderDetails>
I am able to retrieve the information from the root element and first level elements. Using the following code:
 
Dom.Document doc = new Dom.Document();
doc.load(xml);

//Retrieve the root element for this document.
Dom.XMLNode root = doc.getRootElement();
    
String description = root.getName();
String name = root.getChildElement('ProductCode', null).getText();

However, I am not able to retrieve information from the second level (children of the GiftWrap element). I tried using:
 
String name = root.getChildElement('GiftWrap', null).getChildElement('GiftWrapCost', null).getText();

Unfortunately, this does not work. Can someone explain how to achieve this?

 
Best Answer chosen by Frederik Boot
Alain CabonAlain Cabon
apex-xpath: A utility class for Salesforce Apex that lets you easily query an XML DOM structure using a simple subset of XPath syntax.

https://github.com/JenniferSimonds/apex-xpath

I didn't test this tool for XPath but it is the proof that it doesn't exist by default (perhaps for the coming versions).

Alain

All Answers

Alain CabonAlain Cabon
Hi,

There is not XPath expressions in Apex.

The generic code for processing multi-levels XML is often like that:   http://developer.force.com/cookbook/recipe/parsing-xml-using-the-apex-dom-parser   (recursion)

A verbose code could be like that (node by node):
String xml = '<OrderDetails>' +
'<OrderDetailID>2584</OrderDetailID>' +
'<ProductCode>AS-456</ProductCode>' +
'<GiftWrap>' +
'<GiftWrapCost>0.0000</GiftWrapCost>' +
'<GiftWrapType>Paper</GiftWrapType>' +
'</GiftWrap>' +
'</OrderDetails>';
Dom.Document doc = new Dom.Document();
doc.load(xml);
Dom.XMLNode Root = doc.getRootElement();     
String ProductCode = Root.getChildElement('ProductCode', null).getText();
Dom.XMLNode GiftWrapNode = Root.getChildElement('GiftWrap', null);
String GiftWrapType = GiftWrapNode.getChildElement('GiftWrapType', null).getText();
String GiftWrapCost = GiftWrapNode.getChildElement('GiftWrapCost', null).getText();
System.debug('ProductCode: ' + ProductCode);
System.debug('GiftWrapType: ' +  GiftWrapType);
System.debug('GiftWrapCost: ' +  GiftWrapCost);

Best regards

Alain

 
Alain CabonAlain Cabon
apex-xpath: A utility class for Salesforce Apex that lets you easily query an XML DOM structure using a simple subset of XPath syntax.

https://github.com/JenniferSimonds/apex-xpath

I didn't test this tool for XPath but it is the proof that it doesn't exist by default (perhaps for the coming versions).

Alain
This was selected as the best answer
Frederik BootFrederik Boot
Thank you for your reply. The XPath tool works great!