[英]Basic XML parsing issues. Cannot find node names via Java XmlStreamReader method. Any ideas?
Having no luck in parsing some basic XML. 不能解析一些基本的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. 我正在使用Apex语言进行此操作,但是在语法上与Java几乎相同,在这种情况下,使用java.xml.stream.XMLStreamReader作为其XML解析引擎。
The problem is: I'm having no luck getting to any of the actual XML node names. 问题是:我无法获得任何实际的XML节点名称。 The getLocalName() method within the XmlStreamReader class always returns null for all nodes as I loop through them. 当我遍历所有节点时,XmlStreamReader类中的getLocalName()方法始终为所有节点返回null。
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()). 如果运行此命令,将会看到reader.getLocalName()始终返回null,所有随附的方法(getNameSpace(),getLocation(),getPrefix())也将返回null。
Any ideas why? 有什么想法吗? I'm stuck with the XML arriving in the format it's in...so I have to parse it as-is. 我对XML以它所采用的格式感到困惑...所以我必须按原样解析它。 I could use various workarounds (regEx, counting nodes, etc.) but those are messy and not ideal. 我可以使用各种解决方法(regEx,计数节点等),但是这些方法很凌乱,并不理想。
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. 我运行代码,然后过滤执行日志以显示USER_DEBUG语句。 The output shows node names and text as you would expect. 输出将按预期显示节点名称和文本。 I think the key is to use the APEX methods hasText() and hasName(). 我认为关键是使用APEX方法hasText()和hasName()。
String XML_STR = '<document>' + '<result>success</result>' +'<resultcode>000000</resultcode>' +
'<note></note>' + '<item>' +'<quantity>1</quantity>' +
'<fname>Bob</fname>' +'<lname>Tungsten</lname>' +
'<address>23232 Fleet Street</address>' +'<city>Santa Clara</city>' +
'<state>CA</state>' +'<zip>94105</zip>' +
'<country>United States</country>' +'<email>blahblahblah@blahblahblah.com</email>' +
'<phone>4155555555</phone>' +'</item>' +'</document>';
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 这是基于乔恩·芒特乔伊(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() > 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>' + '<result>success</result>' +'<resultcode>000000</resultcode>' +
'<note></note>' + '<item>' +'<quantity>1</quantity>' +
'<fname>Bob</fname>' +'<lname>Tungsten</lname>' +
'<address>23232 Fleet Street</address>' +'<city>Santa Clara</city>' +
'<state>CA</state>' +'<zip>94105</zip>' +
'<country>United States</country>' +'<email>blahblahblah@blahblahblah.com</email>' +
'<phone>4155555555</phone>' +'</item>' +'</document>';
System.debug(parse(XML_STR));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.