转换 XML 字符串包含 HTML 实体

Convert XML string contains HTML entity

提问人:Ragul 提问时间:9/1/2023 最后编辑:Ragul 更新时间:9/1/2023 访问量:48

问:

我有以下 XML 字符串,我需要将标签中的值提取到 HTML 表中<parameterizedString>

const strXML = '<steps id="0" last="4"><step id="1" type="ValidateStep"><parameterizedString isformatted="true">&lt;DIV&gt;&lt;P&gt;Action Field 001&lt;/P&gt;&lt;/DIV&gt;</parameterizedString><parameterizedString isformatted="true">&lt;DIV&gt;&lt;P&gt;Expected Result Field 001&lt;/P&gt;&lt;/DIV&gt;</parameterizedString><description/></step></steps>';

const doc = new DOMParser().parseFromString(strXML, "application/xml");
console.log(doc.documentElement.textContent)

当我解析它时,它抛出错误,

甚至尝试使用以下函数替换字符串,仍然失败。

function decodeEntities(encodedString) {
    var translate_re = /&(nbsp|amp|quot|lt|gt);/g;
    var translate = {
        "nbsp":" ",
        "amp" : "&",
        "quot": "\"",
        "lt"  : "<",
        "gt"  : ">"
    };
    return encodedString.replace(translate_re, function(match, entity) {
        return translate[entity];
    }).replace(/&#(\d+);/gi, function(match, numStr) {
        var num = parseInt(numStr, 10);
        return String.fromCharCode(num);
    });
}
JavaScript HTML JSON XML 编码

评论

0赞 Ragul 9/1/2023
谢谢,但是如何从<parameterizedString>标签中提取数据?

答:

1赞 mplungjan 9/1/2023 #1

以下是如何以推荐的方式创建表格 - 如果首先清理 textContent,innerHTML 语句可能会更安全。

const strXML = `<steps id="0" last="4"><step id="1" type="ValidateStep"><parameterizedString isformatted="true">&lt;DIV&gt;&lt;P&gt;Action Field 001&lt;/P&gt;&lt;/DIV&gt;</parameterizedString><parameterizedString isformatted="true">&lt;DIV&gt;&lt;P&gt;Expected Result Field 001&lt;/P&gt;&lt;/DIV&gt;</parameterizedString><description/></step><step id="2" type="RunStep"><parameterizedString isformatted="true">&lt;DIV&gt;&lt;P&gt;Action Field 001&lt;/P&gt;&lt;/DIV&gt;</parameterizedString><parameterizedString isformatted="true">&lt;DIV&gt;&lt;P&gt;Expected Result Field 001&lt;/P&gt;&lt;/DIV&gt;</parameterizedString><description/></step></steps>`;


const doc = new DOMParser().parseFromString(strXML, "application/xml");

const steps = [...doc.querySelectorAll('step')]

const table = document.createElement("table");
const tbody = document.createElement("tbody");
steps.forEach(step => { console.log(step.querySelector("parameterizedString").textContent)
  const row = document.createElement("tr");
  const cell1 = document.createElement("td");
  cell1.textContent = step.getAttribute("type");
  const cell2 = document.createElement("td");
  cell2.innerHTML = step.querySelector("parameterizedString").textContent;
  row.append(cell1)
  row.append(cell2)
  tbody.append(row)
})
table.append(tbody);
document.body.append(table);

// const fragment = document.createElement("div");
// fragment.innerHTML = doc.documentElement.textContent;
// console.log(fragment.innerHTML)
// document.body.append(fragment)
td { border: 1px solid black; }
table div p { margin:0; padding:0;}

评论

0赞 Ragul 9/1/2023
如果我们有多个 <steps><step id=1><step id=2><steps>,如何在 HTML 表格中制作?
0赞 mplungjan 9/1/2023
查看更新后的代码
0赞 Ragul 9/1/2023
干得好,但我需要这个数据透视表格式,ValidateStep|RunStep 操作字段 001 |操作字段 001 操作字段 002 |操作字段 002
0赞 mplungjan 9/1/2023
好的,付钱给我。SO 不是 elance 网站;)您应该能够通过更改我的代码来自己修复它
2赞 Ragul 9/1/2023
对不起,我会尝试的,感谢您的支持