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>[email protected]</login>
         <email>[email protected]</email>
         <display-uid>[email protected]</display-uid>
       </principal>
       ...
      </principal-list>
    </results>

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

         <name>Research</name>
         <login>[email protected]</login>
         <email>[email protected]</email>
         <display-uid>[email protected]</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();}
            }
        }
    }

1 Answer 1

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>[email protected]</login>'
    +         '<email>[email protected]</email>'
    +         '<display-uid>[email protected]</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();
}
7
  • I am getting an error on List<Dom.XmlNode> principals = principalList.getChildElements(); It's telling: 'Attempt to de-reference a null object '
    – AlphaBravo
    Commented Jan 15, 2014 at 21:17
  • 1
    Disregard my comment above, I had a spelling error. Thank you! everything worked great!
    – AlphaBravo
    Commented Jan 15, 2014 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
    Commented Jan 16, 2014 at 14:41
  • have you initialized the list with a non-null value? eg public List<String> names = new List<String>(); Commented Jan 16, 2014 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
    Commented Jan 16, 2014 at 14:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .