无法读取未定义的属性(读取 'childNodes')
Cannot read properties of undefined (reading 'childNodes')
我正在尝试将文本解析为 text/xml 并获取子节点内的值,但给我这个错误(无法读取未定义的属性(读取 'childNodes') .
我想要 GetValidUserPasswordResult 中的值 true。
这是我正在制作的代码:
var text = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetValidUserPasswordResponse xmlns="http://microsoft.com/webservices/"><GetValidUserPasswordResult>true</GetValidUserPasswordResult></GetValidUserPasswordResponse></soap:Body></soap:Envelope>';
console.log(text);
parser = new DomParser();
xmlDoc = parser.parseFromString(text, "text/xml");
xmlDoc1 = xmlDoc.getElementsByName("GetValidUserPasswordResult")[0].childNodes[0].text;
console.log(xmlDoc1)
我已经找到答案了,我在node.js上做的,但是DOMParser在节点上的实现是xmldom,所以结果是这个
var DOMParser = require('xmldom').DOMParser;
var parser = new DOMParser();
var document = parser.parseFromString('<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetValidUserPasswordResponse xmlns="http://microsoft.com/webservices/"><GetValidUserPasswordResult>true</GetValidUserPasswordResult></GetValidUserPasswordResponse></soap:Body></soap:Envelope>', 'text/xml');
var xmlDoc1 = document.getElementsByTagName("GetValidUserPasswordResult")[0].childNodes[0].nodeValue;
console.log(xmlDoc1)
我正在尝试将文本解析为 text/xml 并获取子节点内的值,但给我这个错误(无法读取未定义的属性(读取 'childNodes') . 我想要 GetValidUserPasswordResult 中的值 true。 这是我正在制作的代码:
var text = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetValidUserPasswordResponse xmlns="http://microsoft.com/webservices/"><GetValidUserPasswordResult>true</GetValidUserPasswordResult></GetValidUserPasswordResponse></soap:Body></soap:Envelope>';
console.log(text);
parser = new DomParser();
xmlDoc = parser.parseFromString(text, "text/xml");
xmlDoc1 = xmlDoc.getElementsByName("GetValidUserPasswordResult")[0].childNodes[0].text;
console.log(xmlDoc1)
我已经找到答案了,我在node.js上做的,但是DOMParser在节点上的实现是xmldom,所以结果是这个
var DOMParser = require('xmldom').DOMParser;
var parser = new DOMParser();
var document = parser.parseFromString('<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetValidUserPasswordResponse xmlns="http://microsoft.com/webservices/"><GetValidUserPasswordResult>true</GetValidUserPasswordResult></GetValidUserPasswordResponse></soap:Body></soap:Envelope>', 'text/xml');
var xmlDoc1 = document.getElementsByTagName("GetValidUserPasswordResult")[0].childNodes[0].nodeValue;
console.log(xmlDoc1)