我正在嘗試將文本決議為 text/xml 并獲取子節點內部的值,但給我這個錯誤(無法讀取未定義的屬性(讀取'childNodes')。我希望值在GetValidUserPasswordResult. 這是我正在制作的代碼:
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)
uj5u.com熱心網友回復:
我已經找到了答案,我在 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/443194.html
標籤:javascript xml 解析器 子节点
