XML Parsing in Salesforce

X
<?xml version='1.0' encoding='iso-8859-1' ?>
<Root>
<Record>
    <CateID>1</CateID>
    <Name>NameABC</Name>
    <Phone>12356</Phone>
    <Email>info@gmail.com</Email>
    <Website>http://www.nameabc.com/page.html</Website>
</Record>
<Record>
    <CateID>2</CateID>
    <Name>Namexyz</Name>
    <Phone>456879</Phone>
    <Email>info@ymail.com</Email>
    <Website>http://www.namexyz.com/page.html</Website>
</Record>
</Root>

// consider, This above XML getting from Listing()

public void Listing()
{     
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://demos.sfxmlpase.com/xml');
        req.setMethod('GET');
        HttpResponse res = http.send(req);         
        String xmlContent = res.getBody();         
        XmlStreamReader reader = res.getXmlStreamReader();    
        XMLData = XMLParser(res.getBody());
     }
public List<String> XMLParser(String strXml)
    {             
       
        List<String> xmlstring =new List<String>();
        Dom.Document doc = new Dom.Document();
        doc.load(strXml);
        Dom.XMLNode Envelope = doc.getRootElement();
        for(integer i=0; i < Envelope.getChildElements().size(); i++)
        {
            Dom.XMLNode Body = Envelope.getChildElements()[i];
            for(Dom.XMLNode child : Body.getChildElements())
            {
                String Text = child.getText();
                if(child.getText().contains('%23'))
                Text = child.getText().replace('%23', '&');
               
                xmlstring.add(Text);
            }
        }
       
    return xmlstring;
    }        //XMLData in Listing() will have all the text inside the tags in list

About the author

kalpesh.surana
By kalpesh.surana

Category