I am trying to parse an xml and get its root element but when I try it Im getting all the elements as a result of getrootelement() method.

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();

  Dom.XmlNode results = doc.getRootElement();

According to the above example the root element must be "results" and since im not iterating further my debug log should show only "results" but im getting the entire xml as output.Where im going wrong ? or my way of understanding is wrong?


It looks like you are getting the root element, but your debug statement is outputting the whole Dom.XmlNode object, whereas you only want the name of the root element.

Try replacing the system.debug statement with system.debug(results.getName());

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.