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

java xml將節點轉換為元素

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

問題描述

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

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

相關文檔推薦

Upload progress listener not fired (Google drive API)(上傳進度偵聽器未觸發(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 示例中缺少)
主站蜘蛛池模板: 亚洲一区二区国产 | 一区二区三区视频在线观看 | 美女久久 | 深夜爽视频 | 欧美二区在线 | av一级久久 | 91精品国产综合久久久久久漫画 | 国产一区二区在线免费视频 | 男人天堂手机在线视频 | 精品国产一二三区 | 成人久久久 | 欧美黄色一区 | 精品久久久久久亚洲精品 | 男女羞羞视频在线 | 欧美日韩亚洲二区 | 国产精品一区二区三区久久久 | 一区二区三区久久 | 奇米超碰 | 毛片a | 欧美日韩国产一区 | 操视频网站 | 精区3d动漫一品二品精区 | 成人精品一区 | 国产精品一区二区久久 | 国产一级视频在线观看 | 亚洲一区二区三区免费视频 | 国产精品99久久久久久动医院 | 国产精品一区二区在线 | www.日日操 | 人人看人人搞 | 99精品在线观看 | 亚洲欧美网站 | 蜜桃av一区二区三区 | 久久国产区 | 久久影音先锋 | 免费久久网站 | 一级爱爱片 | 欧美激情综合 | 色约约视频 | 欧美一级免费黄色片 | 狠狠躁躁夜夜躁波多野结依 |