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
KSKumaarKSKumaar 

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.
<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
 
Hans LissHans Liss
a> That code assumes that the XML document is served by a web server somewhere. The URL is the URL that will provide the document. If you already have the document in a string, you will have to create the Document object in another way (remove row 7-12 and rewrite row 13). Start here (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_xml_dom_document.htm#apex_Dom_Document_load).
b> The XML document you posted doesn't have any namespaces defined