問題描述
我知道這被問了很多次,但我仍然無法讓它工作.我將 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 對象的節(jié)點類型時,它顯示 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,這樣我就可以執(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>
提前致謝!
推薦答案
當我將 Node 對象 n 轉換為 Element 時,我得到一個異常 java.lang.ClassCastException: org.apache.harmony.xml.dom.TextImpl cannot be cast to org.w3c.dom.Element.當我檢查 Node 對象的節(jié)點類型時,它顯示 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?
可能不是,解析器可能是正確的.這意味著您正在解析的某些節(jié)點是 文本節(jié)點.例如:
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>
在上面,我們有一個包含文本節(jié)點的 foo
元素.(文本節(jié)點包含文本 "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
和這些子節(jié)點(按順序):
If your XML document literally looks like the above, it contains a root element foo
with these child nodes (in order):
- 一個帶有空格的文本節(jié)點
- 一個
bar
元素 - 一個包含更多空格的文本節(jié)點
請注意,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將節(jié)點轉換為元素的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!