我知道这被问了很多次,但我仍然无法让它工作.我将 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 cannot be cast to 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 cannot be cast to 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 文档 literally 与上面类似,则它包含一个根元素 foo 和这些子节点(按顺序):
If your XML document literally looks like the above, it contains a root element foo with these child nodes (in order):
bar 元素请注意,bar 元素不是 foo 的第一个子元素.如果它看起来像这样:
Note that the bar element is not the first child of foo. If it looked like this:
<foo><bar>baz</bar></foo>
那么 bar 元素将是 foo 的第一个子元素.
then the bar element would be the first child of foo.
这篇关于java xml将节点转换为元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
上传进度侦听器未触发(Google 驱动器 API)Upload progress listener not fired (Google drive API)(上传进度侦听器未触发(Google 驱动器 API))
使用 Google Drive SDK 将文件保存在特定文件夹中Save file in specific folder with Google Drive SDK(使用 Google Drive SDK 将文件保存在特定文件夹中)
Google Drive Android API - 无效的 DriveId 和 Null ResourcGoogle Drive Android API - Invalid DriveId and Null ResourceId(Google Drive Android API - 无效的 DriveId 和 Null ResourceId)
谷歌驱动api服务账户查看上传文件到谷歌驱动使Google drive api services account view uploaded files to google drive using java(谷歌驱动api服务账户查看上传文件到谷歌驱动使用java
Google Drive 服务帐号返回 403 usageLimitsGoogle Drive service account returns 403 usageLimits(Google Drive 服务帐号返回 403 usageLimits)
com.google.api.client.json.jackson.JacksonFactory;Google Drcom.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example(com.google.api.client.json.jackson.JacksonFactory;Google Drive 示例