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
saikrishna.Gsaikrishna.G 

XML paring

How to parse the response 
 <?xml version=\"1.0\" encoding=\"UTF-8\"?><methodResponse xmlns:ex="http://ws.apache.org/xmlrpc/namespaces/extensions"><params><param><value><array><data><value><boolean>1</boolean></value><value>OK</value></data></array></value></param></params></methodResponse>

I wanted to write logic based on Boolean value in response but am unable to parse this one 
 
Best Answer chosen by saikrishna.G
pconpcon
Because of the structure of the XML, there's no good way to get that.  It's just in a value element so it's tougher.  The code below will work, but it's pretty fragile.
 
String xml = ' <?xml version=\"1.0\" encoding=\"UTF-8\"?><methodResponse xmlns:ex="http://ws.apache.org/xmlrpc/namespaces/extensions"><params><param><value><array><data><value><boolean>1</boolean></value><value>OK</value></data></array></value></param></params></methodResponse>';

Dom.Document doc = new Dom.Document();
doc.load(xml);
Integer boolVal;
String textVal;

for (Dom.XmlNode node : doc.getRootElement().getChildren().get(0).getChildren().get(0).getchildren().get(0).getchildren().get(0).getchildren()) {
    for (Dom.XmlNode node2 : node.getChildren()) {
        if (
            !node2.getChildren().isEmpty() &&
            node2.getchildren().get(0).getName() == 'boolean'
        ) {
            boolVal = Integer.valueOf(node2.getchildren().get(0).getText());
        } else {
            textVal = node2.getText();
        }
    }
}

System.debug(System.LoggingLevel.ERROR, boolVal);
System.debug(System.LoggingLevel.ERROR, textVal);

 

All Answers

pconpcon
This is actually a pretty terrible solution, but the best way is to make a recursive XML parser and that's out of the scope of this.
 
String xml = ' <?xml version=\"1.0\" encoding=\"UTF-8\"?><methodResponse xmlns:ex="http://ws.apache.org/xmlrpc/namespaces/extensions"><params><param><value><array><data><value><boolean>1</boolean></value><value>OK</value></data></array></value></param></params></methodResponse>';

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

for (Dom.XmlNode node : doc.getRootElement().getChildren().get(0).getChildren().get(0).getchildren().get(0).getchildren().get(0).getchildren()) {
    for (Dom.XmlNode node2 : node.getChildren()) {
        if (
            !node2.getChildren().isEmpty() &&
            node2.getchildren().get(0).getName() == 'boolean'
        ) {
            boolVal = Integer.valueOf(node2.getchildren().get(0).getText());
        }
    }
}

System.debug(System.LoggingLevel.ERROR, boolVal);

 
saikrishna.Gsaikrishna.G
Hi Pcon,

Thanks for the reply can you let me know how we can get the text value? "Ok"?

Thanks,
Saikrishna
pconpcon
Because of the structure of the XML, there's no good way to get that.  It's just in a value element so it's tougher.  The code below will work, but it's pretty fragile.
 
String xml = ' <?xml version=\"1.0\" encoding=\"UTF-8\"?><methodResponse xmlns:ex="http://ws.apache.org/xmlrpc/namespaces/extensions"><params><param><value><array><data><value><boolean>1</boolean></value><value>OK</value></data></array></value></param></params></methodResponse>';

Dom.Document doc = new Dom.Document();
doc.load(xml);
Integer boolVal;
String textVal;

for (Dom.XmlNode node : doc.getRootElement().getChildren().get(0).getChildren().get(0).getchildren().get(0).getchildren().get(0).getchildren()) {
    for (Dom.XmlNode node2 : node.getChildren()) {
        if (
            !node2.getChildren().isEmpty() &&
            node2.getchildren().get(0).getName() == 'boolean'
        ) {
            boolVal = Integer.valueOf(node2.getchildren().get(0).getText());
        } else {
            textVal = node2.getText();
        }
    }
}

System.debug(System.LoggingLevel.ERROR, boolVal);
System.debug(System.LoggingLevel.ERROR, textVal);

 
This was selected as the best answer