简体   繁体   English

解析javascript中的xml数据

[英]Parse xml data in javascript

  1. Do I need to convert the response from servlet which is xml to xmlDoc for parsing and retrieval of certain values? 我是否需要将servlet的响应从xml转换为xmlDoc以便解析和检索某些值?
  2. If yes, then is the below code correct? 如果是,那么下面的代码正确吗? console.log(id); prints a function and thus a TypeError is thrown. 打印一个函数,因此引发TypeError。 If no, then how to do it? 如果没有,该怎么办?
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);
}

No, you don't need to convert response, because you can get xmlDoc directly by responseXML property. 不,您不需要转换响应,因为您可以直接通过responseXML属性获取xmlDoc

Example: 例:

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