0

I want to parse the XML body and want to capture the url from the xml body in the list of string which i have created but i am getting Null values in the list

Respnse body:-

 <retours>
  <flux>
    <id>WSfv3vWBFA55L6RMQ28FOh9</id>
    <retour>
      <publication>20181026114057</publication>
      <production>20181026114057</production>
      <nature>10</nature>
      <statut>OK</statut>
      <id>AE.yxLo5IC</id>
      <url>https://telecharge.com</url>
    </retour>
    <retour>
      <publication>20181026114058</publication>
      <production>20181026114058</production>
      <nature>11</nature>
      <statut>KO</statut>
      <id>APAyxLo6K1</id>
      <url>https://telecharge1.com</url>
    </retour>
  </flux>
</retours>

Below Code in Apex for the parsing:

HttpResponse response1 = http.send(request1);
if (response1.getStatusCode() == 200) {
    //system.debug('Body ---->'+response1.getbody());  
    DOM.Document doc = response1.getBodyDocument();
    //DOM.XmlNode rootNode = doc.getRootElement();
    //parseXML(rootNode);
    list<string> urls=new list<string>();

    for (Dom.XmlNode node : doc.getRootElement().getChildElements()) {
        String url = node.getAttributeValue('url', '');
        //Integer score = Integer.valueOf(node.getAttributeValue('_CreditScore', ''));
        urls.add(url);
    }


    system.debug('urls.size--->'+urls.size());


}
else{
    System.debug('The status code returned was not expected: ' +
                 response1.getStatusCode() + ' ' + response1.getStatus());
    System.debug(response1.getBody());
}
2

The URL is on the "next level down", so your code would look something more like this:

for(Dom.XmlNode fluxNode: doc.getRootElement().getChildElements()) {
  for(Dom.XmlNode fluxChildNode: fluxNode.getChildElements()) {
    if(fluxChildNode.getName() == 'retour') {
      urls.add(fluxChildNode.getChildElement('url',null).getText());
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.