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

java xml將節(jié)點轉換為元素

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

問題描述

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

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

相關文檔推薦

Upload progress listener not fired (Google drive API)(上傳進度偵聽器未觸發(fā)(Google 驅動器 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(谷歌驅動api服務賬戶查看上傳文件到谷歌驅動使用java)
Google Drive service account returns 403 usageLimits(Google Drive 服務帳號返回 403 usageLimits)
com.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example(com.google.api.client.json.jackson.JacksonFactory;Google Drive 示例中缺少)
主站蜘蛛池模板: 牛牛热在线视频 | 91视视频在线观看入口直接观看 | 精品欧美一区二区三区久久久 | 激情欧美一区二区三区中文字幕 | 国产美女精品 | 日韩成人在线网站 | 欧美黄色精品 | 亚洲自拍偷拍av | 久久午夜精品 | 色网站在线 | 国产精品一区视频 | 国产精品不卡视频 | 久久婷婷香蕉热狠狠综合 | 91香蕉视频在线观看 | 丁香久久| 国产黄色网址在线观看 | 91视频播放 | 亚洲成人三区 | 亚洲精品一区中文字幕 | 色av一区二区三区 | 九九亚洲 | 99久久精品国产一区二区三区 | 久久里面有精品 | 国产成人亚洲精品 | 射久久| 国产在线观看一区二区三区 | 成人激情视频在线播放 | 在线观看av中文字幕 | 成人精品一区二区三区中文字幕 | 鸡毛片 | 日韩一级免费电影 | 国产成人jvid在线播放 | 国产精品18久久久久久白浆动漫 | 一本岛道一二三不卡区 | 国产欧美精品一区二区色综合朱莉 | 亚洲综合在线一区二区 | 99re国产视频| 亚洲精品电影 | 亚洲精品视频免费看 | 亚洲高清久久 | 日本三级电影免费观看 |