在javascript中解析xml数据

时间:2013-05-05 10:17:57

标签: javascript xml

  1. 我是否需要将来自servlet的响应(xml)转换为xmlDoc for 解析和检索某些值?
  2. 如果是,那么以下代码是否正确? console.log(id);打印一个函数,因此抛出TypeError。如果没有,那该怎么办?
  3. 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);
    }
    

1 个答案:

答案 0 :(得分:0)

不,您不需要转换回复,因为您可以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...
相关问题