久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

java xml將節(jié)點(diǎn)轉(zhuǎn)換為元素

java xml cast Node to Element(java xml將節(jié)點(diǎn)轉(zhuǎn)換為元素)
本文介紹了java xml將節(jié)點(diǎn)轉(zhuǎn)換為元素的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我知道這被問了很多次,但我仍然無法讓它工作.我將 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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Upload progress listener not fired (Google drive API)(上傳進(jìn)度偵聽器未觸發(fā)(Google 驅(qū)動(dòng)器 API))
Save file in specific folder with Google Drive SDK(使用 Google Drive SDK 將文件保存在特定文件夾中)
Google Drive Android API - Invalid DriveId and Null ResourceId(Google Drive Android API - 無效的 DriveId 和 Null ResourceId)
Google drive api services account view uploaded files to google drive using java(谷歌驅(qū)動(dòng)api服務(wù)賬戶查看上傳文件到谷歌驅(qū)動(dòng)使用java)
Google Drive service account returns 403 usageLimits(Google Drive 服務(wù)帳號(hào)返回 403 usageLimits)
com.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example(com.google.api.client.json.jackson.JacksonFactory;Google Drive 示例中缺少)
主站蜘蛛池模板: 综合久久久 | 国产一区二区久久 | 日韩精品亚洲专区在线观看 | 在线午夜电影 | 在线看片国产精品 | 99成人精品 | 精品一区二区三区中文字幕 | 日韩欧美中文字幕在线观看 | 99热热热热 | 国产综合久久久久久鬼色 | 亚洲精品天堂 | 精品久久国产 | 91网站在线观看视频 | 日韩欧美精品在线播放 | 午夜在线观看免费 | 国产精品久久久久久福利一牛影视 | 91影院在线观看 | 亚洲精久 | a免费视频 | 国久久 | 黄在线免费观看 | 亚洲欧美日韩国产 | www.日本在线观看 | 国产视频不卡一区 | 999精品视频| 欧美综合一区 | 久久精品这里精品 | 久久国产一区 | 亚洲精品福利在线 | 黄色欧美在线 | 欧美视频一区二区三区 | 97人澡人人添人人爽欧美 | 国产精品国产三级国产播12软件 | 99精品国产一区二区青青牛奶 | 日本不卡视频 | 青青草av在线播放 | 成人二区 | 91在线网 | 成人免费看片又大又黄 | av高清毛片| 欧美福利专区 |