Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have reformed your code into one block that can be tested in the workbench's anonymous execution window. I run the code and then filter the Execution Log to show USER_DEBUG statements. The output shows node names and text as you would expect. I think the key is to use the APEX methods hasText() and hasName().</p> <pre><code> String XML_STR = '&lt;document&gt;' + '&lt;result&gt;success&lt;/result&gt;' +'&lt;resultcode&gt;000000&lt;/resultcode&gt;' + '&lt;note&gt;&lt;/note&gt;' + '&lt;item&gt;' +'&lt;quantity&gt;1&lt;/quantity&gt;' + '&lt;fname&gt;Bob&lt;/fname&gt;' +'&lt;lname&gt;Tungsten&lt;/lname&gt;' + '&lt;address&gt;23232 Fleet Street&lt;/address&gt;' +'&lt;city&gt;Santa Clara&lt;/city&gt;' + '&lt;state&gt;CA&lt;/state&gt;' +'&lt;zip&gt;94105&lt;/zip&gt;' + '&lt;country&gt;United States&lt;/country&gt;' +'&lt;email&gt;blahblahblah@blahblahblah.com&lt;/email&gt;' + '&lt;phone&gt;4155555555&lt;/phone&gt;' +'&lt;/item&gt;' +'&lt;/document&gt;'; XmlStreamReader reader = new XmlStreamReader(XML_STR); while (reader.hasNext()) { System.debug('$$$ reader.getEventType(): ' + reader.getEventType()); if (reader.hasName()) { System.debug('$$$ reader.getLocalName(): ' + reader.getLocalName()); // System.debug('$$$ reader.getNamespace(): ' + reader.getNamespace()); // System.debug('$$$ reader.getprefix(): ' + reader.getprefix()); } if (reader.hasText()) { System.debug('$$$ reader.getText(): ' + reader.getText()); } System.debug('$$$ Go to next'); reader.next(); } </code></pre> <p>Here is another solution based on the recipe by Jon Mountjoy <a href="http://developer.force.com/cookbook/recipe/parsing-xml-using-the-apex-dom-parser" rel="nofollow">http://developer.force.com/cookbook/recipe/parsing-xml-using-the-apex-dom-parser</a></p> <pre><code>private String walkThrough(DOM.XMLNode node) { String result = '\n'; if (node.getNodeType() == DOM.XMLNodeType.COMMENT) { return 'Comment (' + node.getText() + ')'; } if (node.getNodeType() == DOM.XMLNodeType.TEXT) { return 'Text (' + node.getText() + ')'; } if (node.getNodeType() == DOM.XMLNodeType.ELEMENT) { result += 'Element: ' + node.getName(); if (node.getText().trim() != '') { result += ', text=' + node.getText().trim(); } if (node.getAttributeCount() &gt; 0) { for (Integer i = 0; i&lt; node.getAttributeCount(); i++ ) { result += ', attribute #' + i + ':' + node.getAttributeKeyAt(i) + '=' + node.getAttributeValue(node.getAttributeKeyAt(i), node.getAttributeKeyNsAt(i)); } } for (Dom.XMLNode child: node.getChildElements()) { result += walkThrough(child); } return result; } return ''; //should never reach here } private String parse(String toParse) { DOM.Document doc = new DOM.Document(); try { doc.load(toParse); DOM.XMLNode root = doc.getRootElement(); return walkThrough(root); } catch (System.XMLException e) { // invalid XML return e.getMessage(); } } String XML_STR = '&lt;document&gt;' + '&lt;result&gt;success&lt;/result&gt;' +'&lt;resultcode&gt;000000&lt;/resultcode&gt;' + '&lt;note&gt;&lt;/note&gt;' + '&lt;item&gt;' +'&lt;quantity&gt;1&lt;/quantity&gt;' + '&lt;fname&gt;Bob&lt;/fname&gt;' +'&lt;lname&gt;Tungsten&lt;/lname&gt;' + '&lt;address&gt;23232 Fleet Street&lt;/address&gt;' +'&lt;city&gt;Santa Clara&lt;/city&gt;' + '&lt;state&gt;CA&lt;/state&gt;' +'&lt;zip&gt;94105&lt;/zip&gt;' + '&lt;country&gt;United States&lt;/country&gt;' +'&lt;email&gt;blahblahblah@blahblahblah.com&lt;/email&gt;' + '&lt;phone&gt;4155555555&lt;/phone&gt;' +'&lt;/item&gt;' +'&lt;/document&gt;'; System.debug(parse(XML_STR)); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload