久久久久久久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 示例中缺少)
主站蜘蛛池模板: 欧美freesex黑人又粗又大 | 亚洲一区二区在线播放 | 国产中文一区二区三区 | 蜜月va乱码一区二区三区 | 99九九视频 | 欧美黄色免费网站 | 欧美国产精品一区二区三区 | 中文字幕精品视频 | 免费久久久久久 | 午夜爽爽爽男女免费观看影院 | 欧美日韩综合一区 | 日本亚洲一区 | 亚洲成人福利在线观看 | 午夜精品久久久久久久久久久久 | 亚洲一区二区精品视频 | 中文字幕精品一区二区三区精品 | 国产精品一区二区免费 | 国产色婷婷久久99精品91 | 免费能直接在线观看黄的视频 | 黄色毛片在线看 | 国产95在线 | av中文字幕在线 | 亚洲一区| 视频三区| 精品久久久久一区二区国产 | 看a级黄色毛片 | 欧美日韩福利 | 亚洲精品1区 | 男人的天堂中文字幕 | 国产农村妇女精品一区 | 亚洲在线成人 | 成人片免费看 | 久久久无码精品亚洲日韩按摩 | 久草综合在线 | 91精品一区二区三区久久久久 | 亚洲日韩欧美一区二区在线 | 成人在线观看免费爱爱 | 亚洲欧美另类在线 | 国产精品亚洲精品日韩已方 | 亚洲国产精品一区二区www | 毛片一区 |