基本的XML解析问题.无法通过Java XmlStreamReader方法找到节点名称.有任何想法吗? [英] Basic XML parsing issues. Cannot find node names via Java XmlStreamReader method. Any ideas?

查看:337
本文介绍了基本的XML解析问题.无法通过Java XmlStreamReader方法找到节点名称.有任何想法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解析一些基本的XML没有运气.我正在使用 Apex语言进行此操作,但是它在语法上几乎与Java相同,在这种情况下使用 java.xml.stream.XMLStreamReader 作为其XML解析引擎.

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.

问题是:我没有运气获得任何实际的XML节点名称.当我遍历所有节点时,XmlStreamReader类中的getLocalName()方法始终为所有节点返回null.

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.

代码在此处

目前非常基本的功能.如果运行此命令,将会看到reader.getLocalName()始终返回null,所有随附的方法(getNameSpace(),getLocation(),getPrefix())也将返回.

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

任何想法为何?我对XML以它所采用的格式感到困惑...所以我必须按原样解析它.我可以使用各种解决方法(regEx,计数节点等),但这些方法很杂乱,并不理想.

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.

推荐答案

我已经将您的代码重整为一个块,可以在工作台的匿名执行窗口中对其进行测试.我运行代码,然后过滤执行日志以显示USER_DEBUG语句.输出将按预期显示节点名称和文本. 我认为关键是使用APEX方法hasText()和hasName().

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>' + '<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();
}

这是基于乔恩·芒特乔(Jon Mountjoy)的食谱的另一种解决方案 http://developer.force.com/cookbook/recipe/parsing-xml-using-the-apex-dom-parser

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

这篇关于基本的XML解析问题.无法通过Java XmlStreamReader方法找到节点名称.有任何想法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆