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
ashok  45ashok 45 

how can we read xml from response

Hi,
   I got a response this, here i want to read the data from the response.
can you please help me in this, can you give sample code for this.
How can we read the data from xml?

<?xml version="1.0" encoding="utf-16"?>
<Getmessage xms:xi="http://www.hello.com" xms:xd="http://www.hello.com">
<errorcode>2002</errorcode>
<errormessage>got error</errormessage>
</GetMessage>
Chandra Prakash PandeyChandra Prakash Pandey
Hi Ashok,

Try this code to parse this xml response:
 
String xml = "<...>your XML Response</...>";

Dom.Document doc = new Dom.Document();
doc.load(xml);

Dom.XmlNode getMessage = doc.getRootElement();
String errorCode = getMessage.getChildElement('errorcode', null).getText();
String errorMessage = getMessage.getChildElement('errormessage', null).getText();
In errorCode and errorMessage string variable you will have your main xml tags content. This code is completely typed according to your xml response, using the same logic you can create a generic function to parse your other xml responses also.

Let me know, If it helps.

Regards,
Chandra Prakash
 
RAM AnisettiRAM Anisetti
string xml ='<?xml version="1.0" encoding="utf-16"?>
<Getmessage xms:xi="http://www.hello.com" xms:xd="http://www.hello.com">
<errorcode>2002</errorcode>
<errormessage>got error</errormessage>
</GetMessage>';
						   
						   
						   
						   
       Dom.Document doc = new Dom.Document();
       doc.load(xml);

        //Retrieve the root element for this document.
        Dom.XMLNode errorroot = doc.getRootElement();

        String errorcode= errorroot.getChildElement('errorcode', null).getText();
        String errormsg = errorroot.getChildElement('errormessage', null).getText();