You need to sign in to do that
Don't have an account?
Frederik Boot
DOM Document - multi level XML
Hello,
I have the following XML:
However, I am not able to retrieve information from the second level (children of the GiftWrap element). I tried using:
Unfortunately, this does not work. Can someone explain how to achieve this?
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?
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
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):
Best regards
Alain
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