You need to sign in to do that
Don't have an account?
KSKumaar
How to pass the XML as a url in DOMParser
I found the following details from this https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_xml_dom.htm
As in the page shows, following one is XML file.
a> How to pass the XML file as a url later as a parameter to parseResponseDom() in above class?
b> String name = address.getChildElement('name', null).getText(); In this line, in the place of namespace why we are giving null value in getChildElement(name,namespace)?
Help is appreciated.
Thank you
As in the page shows, following one is XML file.
<address> <name>Kirk Stevens</name> <street1>808 State St</street1> <street2>Apt. 2</street2> <city>Palookaville</city> <state>PA</state> <country>USA</country> </address>The following one is DomDocument class.
public class DomDocument { // Pass in the URL for the request // For the purposes of this sample,assume that the URL // returns the XML shown above in the response body public void parseResponseDom(String url){ Http h = new Http(); HttpRequest req = new HttpRequest(); // url that returns the XML in the response body req.setEndpoint(url); req.setMethod('GET'); HttpResponse res = h.send(req); Dom.Document doc = res.getBodyDocument(); //Retrieve the root element for this document. Dom.XMLNode address = doc.getRootElement(); String name = address.getChildElement('name', null).getText(); String state = address.getChildElement('state', null).getText(); // print out specific elements System.debug('Name: ' + name); System.debug('State: ' + state); // Alternatively, loop through the child elements. // This prints out all the elements of the address for(Dom.XMLNode child : address.getChildElements()) { System.debug(child.getText()); } } }I understood the above class except the two things which listed below. can you please how to achieve them?
a> How to pass the XML file as a url later as a parameter to parseResponseDom() in above class?
b> String name = address.getChildElement('name', null).getText(); In this line, in the place of namespace why we are giving null value in getChildElement(name,namespace)?
Help is appreciated.
Thank you
b> The XML document you posted doesn't have any namespaces defined