要解析的xml字符串格式如下:
text = "<Result>";
text = text + "<ErrorMessage>OK</ErrorMessage>";
text = text + "<PrintVersion>2</PrintVersion>";
text = text + "</Result>";
返回结果 {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;
};