java在xml文件找不到节点,基本的XML解析问题。通过Java XmlStreamReader方法找不到节点名称。有任何想法吗?...

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.

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.

解决方案

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 = '' + 'success' +'000000' +

'' + '' +'1' +

'Bob' +'Tungsten' +

'

23232 Fleet Street
' +'Santa Clara' +

'CA' +'94105' +

'United States' +'blahblahblah@blahblahblah.com' +

'4155555555' +'' +'';

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

}

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 = '' + 'success' +'000000' +

'' + '' +'1' +

'Bob' +'Tungsten' +

'

23232 Fleet Street
' +'Santa Clara' +

'CA' +'94105' +

'United States' +'blahblahblah@blahblahblah.com' +

'4155555555' +'' +'';

System.debug(parse(XML_STR));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值