首页 > 解决方案 > 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>

标签: javascripthtmlxml

解决方案


问题是您的 HTML 不会转义原始 XML 内容,因此 HTML 的规则适用于它。当内容不被解析为 HTML 时,至少<and&符号应该被转义(到 HTML 实体中),并且应该使用textContent而不是读取内容innerHTML

var responseText = `<html>
<head>
</head>
<body text="#000000">
<ProcessTable id="XMLResponseText">&lt;?xml version="1.0"?>
&lt;nib:ProcessRequestResponse xmlns:nib="http://schemas.embarq.com/NIBSAdapter" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    &lt;ZipCode>&lt;ZipDetail>&lt;Branch>Las Vegas&lt;/Branch>&lt;Service>SWWVLVWWNV&lt;/Service>&lt;City>LAS VEGAS&lt;/City>&lt;State>NV&lt;/State>&lt;County/>&lt;Zip>89166&lt;/Zip>&lt;Territory>T-LVWWNV05&lt;/Territory>&lt;Warehouse>LVWWNVAA&lt;/Warehouse>&lt;RATType>DIALTONE&lt;/RATType>&lt;/ZipDetail>&lt;/ZipCode>&lt;/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);


推荐阅读