4

I am trying to parse a file from our meeting server with XML data. The data is structured like this:

  <results>
    <status code="ok"/>
     <principal-list>
      <principal principal-id="1089714827" account-id="1089571079" type="user" has-children="false" is-primary="false" is-hidden="false" training-group-id="">
         <name>Research</name>
         <login>events@Research.com</login>
         <email>events@Research.com</email>
         <display-uid>events@Research.com</display-uid>
       </principal>
       ...
      </principal-list>
    </results>

I am trying to get the text portion portion of the XML:

         <name>Research</name>
         <login>events@Research.com</login>
         <email>events@Research.com</email>
         <display-uid>events@Research.com</display-uid>

I created a loop in my program that looks like this:

  string enames;
    string etext;
    for (Dom.XMLNode child: row.getChildElements()){
        for(Dom.XmlNode c: child.getChildElements()){
            for(Dom.XmlNode d: c.getChildElements()){
            if(d.getNodeType() == Dom.XmlNodeType.ELEMENT){
           Enames += d.getName();}
        else{
            Etext = d.getText();}
            }
        }
    }

All I get from that is the tag in etext.

Any suggestions would be great. Thanks in advanced.

This is the full request:

            http h = new http();
            httpRequest req = New httpRequest();   
            req.setEndpoint(logBase);
            req.setMethod('GET');
            req.setHeader('Cookie', 'BREEZESESSION=' + session);
            httpResponse res = h.send(req);

           System.debug(res.getBody());

           Dom.Document docx = new Dom.Document();
           docx.load(res.getBody());
           dom.XmlNode row = docx.getRootElement();
    string enames;
    string etext;
    for (Dom.XMLNode child: row.getChildElements()){
        for(Dom.XmlNode c: child.getChildElements()){
            for(Dom.XmlNode d: c.getChildElements()){
            if(d.getNodeType() == Dom.XmlNodeType.ELEMENT){
           Enames += d.getName();}
        else{
            Etext = d.getText();}
            }
        }
    }
5

You're pretty close! The nested loops are a bit of a confuser, but this works alright for me:

String xml = '<results>'
    + '<status code="ok"/>'
    + '<principal-list>'
    +     '<principal principal-id="1089714827" account-id="1089571079" type="user" has-children="false" is-primary="false" is-hidden="false" training-group-id="">'
    +         '<name>Research</name>'
    +         '<login>events@Research.com</login>'
    +         '<email>events@Research.com</email>'
    +         '<display-uid>events@Research.com</display-uid>'
    +     '</principal>'
    + '</principal-list>'
    + '</results>'
;

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

Dom.XmlNode results = doc.getRootElement();
Dom.XmlNode principalList = results.getChildElement('principal-list', null);
List<Dom.XmlNode> principals = principalList.getChildElements();
for (Dom.XmlNode principal : principals) {
    //do stuff in here with each principal
    String name = principal.getChildElement('name', null).getText();
    String login = principal.getChildElement('login', null).getText();
    String email = principal.getChildElement('email', null).getText();
    String displayUid = principal.getChildElement('display-uid', null).getText();
}
  • I am getting an error on List<Dom.XmlNode> principals = principalList.getChildElements(); It's telling: 'Attempt to de-reference a null object ' – AlphaBravo Jan 15 '14 at 21:17
  • 1
    Disregard my comment above, I had a spelling error. Thank you! everything worked great! – AlphaBravo Jan 15 '14 at 21:24
  • I am not sure why, but I am getting a de-reference error when I try to pass the string into a list: String name; String login; String displayUid; for ( dom.XmlNode principal: principals){ name += principal.getChildElement('name', null).getText() +', '; names.add(name); <--- right here I declared the list in my class as public list<string> names {get;set;} – AlphaBravo Jan 16 '14 at 14:41
  • have you initialized the list with a non-null value? eg public List<String> names = new List<String>(); – bigassforce Jan 16 '14 at 14:44
  • 1
    actually scratch that, I forgot I changed the child element I was looking for and it doesn't exist in this set. Sorry again. Thank you for all the help! – AlphaBravo Jan 16 '14 at 14:58

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.