簡體   English   中英

解析javascript中的xml數據

[英]Parse xml data in javascript

  1. 我是否需要將servlet的響應從xml轉換為xmlDoc以便解析和檢索某些值?
  2. 如果是,那么下面的代碼正確嗎? console.log(id); 打印一個函數,因此引發TypeError。 如果沒有,該怎么辦?
function xmlParser(xmlResponse) {
    if (window.DOMParser) {
        parser = new DOMParser();
        console.log(xmlResponse);
        xmlDoc = parser.parseFromString(xmlResponse, "text/xml");
        console.log(xmlDoc);
    }
    id = xmlDoc.getElementsByTagName("id")[0].childNodes[0].nodeValue;
    console.log(id);
    key = xmlDoc.getElementsByTagName("passkey")[0].childNodes[0].nodeValue;
    console.log(key);
    return format(id, key);
}

不,您不需要轉換響應,因為您可以直接通過responseXML屬性獲取xmlDoc

例:

xmlDoc = xmlResponse.responseXML; // you'll probably need to change it because I don't know what is value of xmlResponse in your case
id = xmlDoc.getElementsByTagName("id")[0].childNodes[0].nodeValue;
//and so on...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM