java xml将节点转换为Element [英] java xml cast Node to Element

查看:1106
本文介绍了java xml将节点转换为Element的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这次被问过多次,但我仍然无法让它发挥作用。我将xml字符串转换为Document对象,然后解析它。以下是代码:

I know this was asked many times but I still cannot get it to work. I convert xml string to Document object and then parse it. Here is the code:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try
{
  builder = factory.newDocumentBuilder();  
  Document document = builder.parse( new InputSource( new StringReader( result ) ) );
  Node head = document.getFirstChild();
  if(head != null)
  {                  
    NodeList airportList = head.getChildNodes();      
    for(int i=0; i<airportList.getLength(); i++) {
    Node n = airportList.item(i);                        
    Element airportElem = (Element)n;
  }
}
catch (Exception e) {
  e.printStackTrace();
} 

当我将Node对象n转换为Element时,我得到一个异常java.lang .ClassCastException:org.apache.harmony.xml.dom.TextImpl无法强制转换为org.w3c.dom.Element。当我检查Node对象的节点类型时,它表示Node.TEXT_NODE。我相信它应该是Node.ELEMENT_NODE。我对吗?

When I cast the Node object n to Element I get an exception java.lang.ClassCastException: org.apache.harmony.xml.dom.TextImpl cannot be cast to org.w3c.dom.Element. When I check the node type of the Node object it says Node.TEXT_NODE. I believe it should be Node.ELEMENT_NODE. Am I right?

那么如何将Node转换为Element,所以我可以做一些像element.getAttribute(attrName)这样的事情。

So how do I convert Node to Element, so I can do something like element.getAttribute("attrName").

这是我的XML:

<?xml version="1.0" encoding="utf-8" ?> 
<ArrayOfCity xmlns:xsd="http://www.w3.org/2001/XMLSchema"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<City>
  <strName>Abu Dhabi</strName> 
  <strCode>AUH</strCode> 
</City>
<City>
  <strName>Amsterdam</strName> 
  <strCode>AMS</strCode> 
</City>
<City>
  <strName>Antalya</strName> 
  <strCode>AYT</strCode> 
</City>
<City>
  <strName>Bangkok</strName> 
  <strCode>BKK</strCode> 
</City>
</ArrayOfCity>

提前致谢!

推荐答案


当我将Node对象n转换为Element时,我得到一个异常java.lang.ClassCastException:org.apache.harmony.xml.dom.TextImpl无法强制转换为org .w3c.dom.Element。当我检查Node对象的节点类型时,它表示Node.TEXT_NODE。我相信它应该是Node.ELEMENT_NODE。我是对的吗?

When I cast the Node object n to Element I get an exception java.lang.ClassCastException: org.apache.harmony.xml.dom.TextImpl cannot be cast to org.w3c.dom.Element. When I check the node type of the Node object it says Node.TEXT_NODE. I believe it should be Node.ELEMENT_NODE. Am I right?

可能不是,解析器可能是正确的。这意味着你正在解析的一些节点是 文本节点 。例如:

Probably not, the parser is probably right. It means that some of the nodes in what you're parsing are text nodes. For example:

<foo>bar</foo>

在上面,我们有 foo 包含文本节点的元素。 (文本节点包含文本bar。)

In the above, we have a foo element containing a text node. (The text node contains the text "bar".)

同样,请考虑:

<foo>
    <bar>baz</bar>
</foo>

如果您的XML文档字面如上所示,则它包含一个根元素 foo 带有这些子节点(按顺序):

If your XML document literally looks like the above, it contains a root element foo with these child nodes (in order):


  • 带有文本节点的文本节点其中有一些空格

  • 一个 bar 元素

  • 一个文本节点,其中有一些空格它

  • A text node with some whitespace in it
  • A bar element
  • A text node with some more whitespace in it

请注意 bar 元素不是<$的第一个子元素C $ C> FOO 。如果它看起来像这样:

Note that the bar element is not the first child of foo. If it looked like this:

<foo><bar>baz</bar></foo>

然后 元素将是 foo 的第一个子元素。

then the bar element would be the first child of foo.

这篇关于java xml将节点转换为Element的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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