javascript - Javascript - DOMParser.parseFromString()getElementById().innerHTML 显示空白标签的值
问题描述
我有下面的 responseText,其 xml 内容存储在 ID“XMLResponseText”中。当我使用解析DOMParser.parseFromString
并尝试获取时innerHTML
,空白标签无法正确显示。
示例 - 标签“County”没有值。它显示为自闭合标签。< county />
. 见下文
<html>
<head>
</head>
<body text="#000000">
<ProcessTable id="XMLResponseText"><?xml version="1.0"?>
<nib:ProcessRequestResponse xmlns:nib="http://schemas.embarq.com/NIBSAdapter" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<ZipCode><ZipDetail><Branch>Las Vegas</Branch><Service>SWWVLVWWNV</Service><City>LAS VEGAS</City><State>NV</State><County/><Zip>89166</Zip><Territory>T-LVWWNV05</Territory><Warehouse>LVWWNVAA</Warehouse><RATType>DIALTONE</RATType></ZipDetail></ZipCode></nib:ProcessRequestResponse>
</ProcessTable>
</body>
</html>
然后我有下面的代码。
var parser=new DOMParser();
var xmlDoc = parser.parseFromString(objHTTP.responseText, "text/html");
var XMLResponseTextVal = xmlDoc.getElementById("XMLResponseText").innerHTML;
alert(XMLResponseTextVal)
显示其中包含值的“县”。
<!--?xml version="1.0"?-->
<nib:processrequestresponse xmlns:nib="http://schemas.embarq.com/NIBSAdapter" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<zipcode>
<zipdetail>
<branch>Las Vegas</branch>
<service>SWWVLVWWNV</service>
<city>LAS VEGAS</city>
<state>NV</state>
<county>
<zip>89166</zip>
<territory>T-LVWWNV05</territory>
<warehouse>LVWWNVAA</warehouse>
<rattype>DIALTONE</rattype>
</county>
</zipdetail>
</zipcode>
</nib:processrequestresponse>
解决方案
问题是您的 HTML 不会转义原始 XML 内容,因此 HTML 的规则适用于它。当内容不被解析为 HTML 时,至少<
and&
符号应该被转义(到 HTML 实体中),并且应该使用textContent
而不是读取内容innerHTML
:
var responseText = `<html>
<head>
</head>
<body text="#000000">
<ProcessTable id="XMLResponseText"><?xml version="1.0"?>
<nib:ProcessRequestResponse xmlns:nib="http://schemas.embarq.com/NIBSAdapter" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<ZipCode><ZipDetail><Branch>Las Vegas</Branch><Service>SWWVLVWWNV</Service><City>LAS VEGAS</City><State>NV</State><County/><Zip>89166</Zip><Territory>T-LVWWNV05</Territory><Warehouse>LVWWNVAA</Warehouse><RATType>DIALTONE</RATType></ZipDetail></ZipCode></nib:ProcessRequestResponse>
</ProcessTable>
</body>
</html>`;
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(responseText, "text/html");
var XMLResponseTextVal = xmlDoc.getElementById("XMLResponseText").textContent;
console.log(XMLResponseTextVal);
您发出请求的服务器应该对此进行调整。如果这不可能,则不要将其解析为 HTML,而是从原始响应文本中提取您感兴趣的部分:
var responseText = `<html>
<head>
</head>
<body text="#000000">
<ProcessTable id="XMLResponseText"><?xml version="1.0"?>
<nib:ProcessRequestResponse xmlns:nib="http://schemas.embarq.com/NIBSAdapter" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<ZipCode><ZipDetail><Branch>Las Vegas</Branch><Service>SWWVLVWWNV</Service><City>LAS VEGAS</City><State>NV</State><County/><Zip>89166</Zip><Territory>T-LVWWNV05</Territory><Warehouse>LVWWNVAA</Warehouse><RATType>DIALTONE</RATType></ZipDetail></ZipCode></nib:ProcessRequestResponse>
</ProcessTable>
</body>
</html>`;
var XMLResponseTextVal = responseText.split(/<\/?ProcessTable[^>]*>/g)[1];
console.log(XMLResponseTextVal);
推荐阅读
- firebase - 查询对象的firestore数组
- angular - 如何在`ngFor`循环中添加`checkbox`
- python-3.x - Python导入差异
- javascript - AngularJS ng-repeat 不显示我的数据
- c++ - 将little endian的位转换并屏蔽为big endian
- java - 如何在 createContents 函数之外编辑标签?
- javascript - 在 React 中渲染状态元素
- javascript - Chartjs插件注释示例不起作用
- wso2 - 在 WSO2 Config Lang 中将一个数据类型对象转换为另一种数据类型对象
- python - python-textFSM 解析多行配置