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

查看:24
本文介绍了基本的 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 所采用的格式困住了……所以我必须按原样解析它.我可以使用各种解决方法(正则表达式、计算节点等),但这些方法很混乱且不理想.

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天全站免登陆