js解析xml字符串

要解析的xml字符串格式如下:

    text = "";
    text = text + "OK";
    text = text + "2";
    text = text + "
";

返回结果 {errorInfo: "OK", versionInfo: "2"}

函数如下:

var xmlParse = function(text) {
    try //Internet Explorer
    {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(text);
    } catch (e) {
        try //Firefox, Mozilla, Opera, etc.
        {
            parser = new DOMParser();
            xmlDoc = parser.parseFromString(text, "text/xml");
        } catch (e) { alert(e.message) }
    }
    console.log(xmlDoc);
    var errorMessage = xmlDoc.getElementsByTagName('ErrorMessage')[0].textContent;
    var printVersion = xmlDoc.getElementsByTagName('PrintVersion')[0].textContent;

    var objDate = {};
    objDate.errorInfo = errorMessage;
    objDate.versionInfo = printVersion;
    console.log(objDate);
    return objDate;
};

 

你可能感兴趣的:(js解析xml字符串)