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

無法使用 DOM 解析器讀取帶有命名空間前綴的 x

Unable to read xml with namespace prefix using DOM parser(無法使用 DOM 解析器讀取帶有命名空間前綴的 xml)
本文介紹了無法使用 DOM 解析器讀取帶有命名空間前綴的 xml的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

這是輸入 XML:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <ns2:SendResponse xmlns:ns2="http://mycompany.com/schema/">
         <ns2:SendResult>
            <ns2:Token>A00179-02</ns2:Token>
         </ns2:SendResult>
      </ns2:SendResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

這是我用來讀取 XML 的代碼(變量 xmlString 包含上面的 XML):

This the code that I'm using to read the XML (Variable xmlString contains the XML above):

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlString));
Document doc = db.parse(is);

System.out.println("Element :" + doc.getElementsByTagName("Token").item(0));
System.out.println("Element :" + doc.getElementsByTagName("ns2:Token").item(0));

輸出:

Element :null
Element :[ns2:Token: null]

如果我使用ns2:Token"作為標簽名稱,我可以讀取元素,但我不想在代碼中使用前綴,因為我不確定它是否相同或將來改變.有什么方法可以讀取 xml 元素而無需在標簽名稱中硬編碼命名空間?

I'm able to read the element if I use "ns2:Token" as the tag name, but I don't want to use the prefix in my code as I'm not sure if it'll be the same or change in the future. Is there any way to read the xml element without hard-coding the namespace in the tag name?

推薦答案

W3C dom 命名空間元素的方法:

The W3C dom method for namespaced elements:

getElementsByTagNameNS

NodeList getElementsByTagNameNS(String namespaceURI,
                                String localName)

    Returns a NodeList of all the Elements with a given local name and namespace URI in document order.

    Parameters:
        namespaceURI - The namespace URI of the elements to match on. The special value "*" matches all namespaces.
        localName - The local name of the elements to match on. The special value "*" matches all local names. 
    Returns:
        A new NodeList object containing all the matched Elements.
    Since:
        DOM Level 2

IIRC 早期版本的 W3C DOM 對命名空間的支持很差,所以我不使用它.但是,如果您將上述內容與完整的 namespaceURI http://schemas.xmlsoap.org/soap/envelope/ 一起使用,它應該可以工作.前綴并不重要——它在使用它的文檔之外沒有永久性.

IIRC earlier version of the W3C DOM had poor support for namespaces so I don't use it. However if you use the above with the full namespaceURI http://schemas.xmlsoap.org/soap/envelope/ it should work. The prefix is unimportant - it has no permanency outside the document it is used in.

那就試試吧:

System.out.println("Element :" + doc.getElementsByTagNameNS(
        "http://schemas.xmlsoap.org/soap/envelope/", "Token").item(0));

這篇關于無法使用 DOM 解析器讀取帶有命名空間前綴的 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啪啪网站大全免费观看 | 日韩欧美综合在线视频 | 国产亚洲一区二区三区在线 | 欧美精品一区二区免费 | 日韩在线视频观看 | 成人免费视频网站 | 亚洲一区二区久久久 | 欧美视频免费在线 | 久久国产精品一区二区三区 | 欧美二级 | 久久精品中文 | 日本网站在线看 | 亚洲人精品 | 狠狠狠色丁香婷婷综合久久五月 | 亚洲成人av在线播放 | 国产一区影院 | 精品伊人久久 | 国产精品毛片av一区 | 日本一二三区高清 | 插插插干干干 | 国产精品自产拍在线观看蜜 | 国产农村妇女毛片精品久久麻豆 | 99精品九九 | 亚洲精品一区国产精品 | 麻豆一区二区三区 | 国产精品久久久久久久午夜片 | 超碰成人免费 | 国产精品精品视频一区二区三区 | 国产精品v | 欧美2区| 蜜桃在线播放 | av一区二区三区在线观看 | 国产成人午夜精品影院游乐网 | 亚洲免费在线 | 久久高清| 中文字幕一区二区三区精彩视频 | jizz在线免费观看 | 91精品国产综合久久久久久丝袜 | 免费一区 | 精品一区二区三区电影 | 日本精品视频 |