問題描述
我知道這被問了很多次,但我仍然無法讓它工作.我將 xml 字符串轉(zhuǎn)換為 Document 對(duì)象,然后解析它.代碼如下:
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();
}
當(dāng)我將 Node 對(duì)象 n 轉(zhuǎn)換為 Element 時(shí),我得到一個(gè)異常 java.lang.ClassCastException: org.apache.harmony.xml.dom.TextImpl cannot be cast to org.w3c.dom.Element.當(dāng)我檢查 Node 對(duì)象的節(jié)點(diǎn)類型時(shí),它顯示 Node.TEXT_NODE.我相信它應(yīng)該是 Node.ELEMENT_NODE.我對(duì)嗎?
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 轉(zhuǎn)換為 Element,這樣我就可以執(zhí)行 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>
提前致謝!
推薦答案
當(dāng)我將 Node 對(duì)象 n 轉(zhuǎn)換為 Element 時(shí),我得到一個(gè)異常 java.lang.ClassCastException: org.apache.harmony.xml.dom.TextImpl cannot be cast to org.w3c.dom.Element.當(dāng)我檢查 Node 對(duì)象的節(jié)點(diǎn)類型時(shí),它顯示 Node.TEXT_NODE.我相信它應(yīng)該是 Node.ELEMENT_NODE.我說的對(duì)嗎?
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?
可能不是,解析器可能是正確的.這意味著您正在解析的某些節(jié)點(diǎn)是 文本節(jié)點(diǎn).例如:
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>
在上面,我們有一個(gè)包含文本節(jié)點(diǎn)的 foo
元素.(文本節(jié)點(diǎn)包含文本 "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 與上面類似,則它包含一個(gè)根元素 foo
和這些子節(jié)點(diǎn)(按順序):
If your XML document literally looks like the above, it contains a root element foo
with these child nodes (in order):
- 一個(gè)帶有空格的文本節(jié)點(diǎn)
- 一個(gè)
bar
元素 - 一個(gè)包含更多空格的文本節(jié)點(diǎn)
請(qǐng)注意,bar
元素不是 foo
的第一個(gè)子元素.如果它看起來像這樣:
Note that the bar
element is not the first child of foo
. If it looked like this:
<foo><bar>baz</bar></foo>
那么 bar
元素將是 foo
的第一個(gè)子元素.
then the bar
element would be the first child of foo
.
這篇關(guān)于java xml將節(jié)點(diǎn)轉(zhuǎn)換為元素的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!