Title: Basic XML parsing issues. Cannot find node names via Java XmlStreamReader method. Any ideas?
Tags: java;xml;xml-parsing;apex-code
Question: Having no luck in parsing some basic XML. I'm doing this in the Apex language, but it's syntactically nearly identical to Java and in this case uses java.xml.stream.XMLStreamReader as its XML parsing engine.
The problem is: I'm having no luck getting to any of the actual XML node names. The getLocalName()  method within the XmlStreamReader class always returns null for all nodes as I loop through them.
code is here
Very basic functionality at this point. If you run this, you will see that reader.getLocalName() always returns null and so do all accompanying methods (getNameSpace(), getLocation(), getPrefix()).
Any ideas why? I'm stuck with the XML arriving in the format it's in...so I have to parse it as-is. I could use various workarounds (regEx, counting nodes, etc.) but those are messy and not ideal.
Comment: I suggest that you solve this problem by attempting a much smaller example in pure Java (no apex) that parses the XML the way you want.  By doing so you can determine whether the problem is in your specific java code or in the use of apex code. If the problem persists in the pure java example you'll get a much larger audience here who can help you. Also... please post your code as part of the question.


Here is another answer: 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().
String XML_STR = '<document&gt;' + '<result&gt;success</result&gt;' +'<resultcode&gt;000000</resultcode&gt;' +
'<note&gt;</note&gt;' + '<item&gt;' +'<quantity&gt;1</quantity&gt;' +
'<fname&gt;Bob</fname&gt;' +'<lname&gt;Tungsten</lname&gt;' +
'<address&gt;23232 Fleet Street</address&gt;' +'<city&gt;Santa Clara</city&gt;' +
'<state&gt;CA</state&gt;' +'<zip&gt;94105</zip&gt;' +
'<country&gt;United States</country&gt;' +'<email&gt;cameronmiller@example.org</email&gt;' +
'<phone&gt;385.919.8048</phone&gt;' +'</item&gt;' +'</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();
Here is another solution based on the recipe by Jon Mountjoy
http://developer.force.com/cookbook/recipe/parsing-xml-using-the-apex-dom-parser
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< 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 = '<document&gt;' + '<result&gt;success</result&gt;' +'<resultcode&gt;000000</resultcode&gt;' +
'<note&gt;</note&gt;' + '<item&gt;' +'<quantity&gt;1</quantity&gt;' +
'<fname&gt;Bob</fname&gt;' +'<lname&gt;Tungsten</lname&gt;' +
'<address&gt;23232 Fleet Street</address&gt;' +'<city&gt;Santa Clara</city&gt;' +
'<state&gt;CA</state&gt;' +'<zip&gt;94105</zip&gt;' +
'<country&gt;United States</country&gt;' +'<email&gt;cameronmiller@example.org</email&gt;' +
'<phone&gt;385.919.8048</phone&gt;' +'</item&gt;' +'</document&gt;';
System.debug(parse(XML_STR));

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:12/03/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><xml><xml-parsing><apex-code>', 'Title': 'Basic XML parsing issues. Cannot find node names via Java XmlStreamReader method. Any ideas?', 'ViewCount': 2130, 'id': 13675702, 'user_AboutMe': None, 'user_AccountId': 1246786.0, 'user_DisplayName': 'doubleminus', 'user_Reputation': 11.0, 'user_Views': 6.0, 'user_id': 1871301.0}}