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
RedSalesRedSales 

XMLStreamReader - How To Get A Specific Attribute/Element value

Hello,

 

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 but I'm still unsure as to what's required.

 

My SOAP response XMl is similar to the following

 

 

<?xml version="1.0" encoding="utf-8"?>
  <soap:Body>
    <user_createResponse xmlns="http://dummyValue">
     <user_createResult>28</user_createResult>
    </user_createResponse>
  </soap:Body>
</soap:Envelope>
I tried to use 
XmlStreamReader xmlRead =  res.getXmlStreamReader();
System.debug('key val = ' + xmlRead.getAttributeValue('', 'user_createResult'));
But I get an error such as "illegal State: Current state is not among the states START_ELEMENT , ATTRIBUTEvalid for getAttributeValue(...."
is it not possible to just name the attribute I want or is it necessary to somehow loop through each element or go to an element number?
Any tips on the above would be welcome.
Thanks!

 

Best Answer chosen by Admin (Salesforce Developers) 
Afzal MohammadAfzal Mohammad

I suggest you to use Dom.Document and Dom.XMLNode classes.

 

I have written below class for you. You may use it, as it is or change it per your need.

 

 

public class rnd{
private string strXml = '<?xml version="1.0" encoding="utf-8"?>'+
'<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">'+
'  <soap:Body>'+
'    <user_createResponse xmlns="http://dummyValue">'+
'     <user_createResult>28</user_createResult>'+
'    </user_createResponse>'+
'  </soap:Body>'+
'</soap:Envelope>';

    public rnd(){    
        Dom.Document doc = new Dom.Document();
        doc.load(strXml);    
        //Retrieve the root element for this document.
        Dom.XMLNode Envelope = doc.getRootElement();        
        Dom.XMLNode Body= Envelope.getChildElements()[0];        
        string user_createResult = '';
        for(Dom.XMLNode child : Body.getChildElements()) {
            for(Dom.XMLNode subchild : child.getChildElements()) {
                user_createResult = subchild.getText();
            }
        }                
        System.debug('####user_createResult values is: ' + user_createResult);
    }
}

 Hope that helps.

 

Afzal

 

All Answers

Afzal MohammadAfzal Mohammad

I suggest you to use Dom.Document and Dom.XMLNode classes.

 

I have written below class for you. You may use it, as it is or change it per your need.

 

 

public class rnd{
private string strXml = '<?xml version="1.0" encoding="utf-8"?>'+
'<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">'+
'  <soap:Body>'+
'    <user_createResponse xmlns="http://dummyValue">'+
'     <user_createResult>28</user_createResult>'+
'    </user_createResponse>'+
'  </soap:Body>'+
'</soap:Envelope>';

    public rnd(){    
        Dom.Document doc = new Dom.Document();
        doc.load(strXml);    
        //Retrieve the root element for this document.
        Dom.XMLNode Envelope = doc.getRootElement();        
        Dom.XMLNode Body= Envelope.getChildElements()[0];        
        string user_createResult = '';
        for(Dom.XMLNode child : Body.getChildElements()) {
            for(Dom.XMLNode subchild : child.getChildElements()) {
                user_createResult = subchild.getText();
            }
        }                
        System.debug('####user_createResult values is: ' + user_createResult);
    }
}

 Hope that helps.

 

Afzal

 

This was selected as the best answer
RedSalesRedSales

Thanks for the help. Much appreciated!

TechnozChampTechnozChamp

Thanks .It helped a lot.

SpoorthySpoorthy
This code helped me in my implementation too. Thank you very much.