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
B2AB2A 

Reading in a URL for parsing

So far in my apex playing time I have been able to parse out values from a string of characters

private void testBookParser() { XmlStreamReaderDemo demo = new XmlStreamReaderDemo(); String str = '<books><book author="Manoj">Foo bar</book>' + '<book author="Mysti">Baz</book></books>'; XmlStreamReader reader = new XmlStreamReader(str); books = demo.parseBooks(reader); //System.debug(books.size()); //for (Book book : books) {System.debug(book); //} }

 

 

However, syntatically how can I read in a URL string which contains XML (eg. 'https://na6.salesforce.com/servlet/servlet.ReportList') so that I can parse it?

 

Any help would be appreciated!

EtienneCoutantEtienneCoutant

From the Documentation:

 

 

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());
        }
    }
}