+ Start a Discussion
Arjun SrivastavaArjun Srivastava 

XMLStreamReader - How To Get A Attribute/Element value!



I have an apex class in which i call a web service. I receive a response value from the webservice in which I need to extract a key value from. I've been looking at the XMLStreamReader documentation at http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_xml_XmlStream_reader.htm

My Rest Api Xml response is :

String response=

<xml version='1.0' encoding='UTF-8' ?>
<tree><folder id="0" name=""  >
<folder id="297301978" name="Bicky Rista" ></folder>
<folder id="297301898" name="Jason"  ></folder>

<folder id="297301898" name="Box.net"  ></folder>


I am doing this:


  XmlStreamReader  reader = new XmlStreamReader(response);

            // Read through the XML  
            //and extract the folderid
            while(reader.hasNext()) {
             // System.debug('Event Type:' + reader.getEventType());
            if (reader.getEventType() == XmlTag.START_ELEMENT &&  reader.getlocalname()=='folder') {
                  // System.debug('=================='+reader.getText());
                 // FolderID= reader.getText();


Above am getting the id of the Ist folder inside Parent Folder.

As you guys can see Parent Folder has 3 Subfolders.

I want to know to the id of the sub- folder on the basis of the foldername.How can i achieve this.?



Best Answer chosen by Admin (Salesforce Developers) 
Arjun SrivastavaArjun Srivastava

Yes It's easier through XML Dom library.

All Answers


I think it would be easier if you used the DOM class instead.  Then you could use the getChildElements method to get the subfolders of each parent folder.

Arjun SrivastavaArjun Srivastava

Yes It's easier through XML Dom library.

This was selected as the best answer