简体   繁体   中英

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?
  2. If yes, then is the below code correct? console.log(id); prints a function and thus a TypeError is thrown. 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.

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM