从XML Doc的NodeList中删除节点

时间:2019-04-12 21:47:36

标签: javascript node.js xml parsing

我正在尝试从XML文档中删除某些节点(如果它们存在于以下数组中):

 const removeNodesDataMap = [
    'Source',
    'ProductCode',
    'ProductCategory',
    'PublicationDateTime',
    'ArticleID',
    'Author'
 ];

我尝试使用xmldom npm软件包和removeChild方法来执行此操作,如下所示:

function removeXmlNodes(str) {
    const xmlValue = new DomParser().parseFromString(str, 'text/xml');
    removeNodesDataMap.forEach(node => {
        const rNode = xmlValue.getElementsByTagName(node)[0];
        if (rNode) {
            const sNode = xmlValue.removeChild(rNode.parentNode);
        }
    });
    const serializer = XMLSerializer.serializeToString(xmlValue);
    return serializer;
}

但是,序列化器仍然包含所有节点。基于节点是否与上面的数组相匹配的最佳删除方法是什么?

XML示例:

<Source>ABC</Source>
<ProductCode>77</ProductCode>
<ProductCategory>Performance</ProductCategory>
<PublicationDateTime>2019-03-06  17:04:5.000</PublicationDateTime>
<ArticleID>123254</ArticleID>
<Author/>
<records>
<record>some record stuff that I actually want to return along with node</record>
</records>

预期输出为:

"<records><record>some record stuff that I actually want to return along with node</record></records>"

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

当缺少根目录时,它似乎对您的xml有点奇怪。我有点可以解决您的问题,但我需要将XML内容包装在根目录中。主要更改是通过调用node.ParentNode.remove(node)

来删除
const { DOMParser, XMLSerializer } = require('xmldom');

const removeNodesDataMap = [
  'Source',
  'ProductCode',
  'ProductCategory',
  'PublicationDateTime',
  'ArticleID',
  'Author',
];

function removeXmlNodes(str) {
  const xmlValue = new DOMParser().parseFromString(`<root id="root">${str}</root>`, 'text/xml');
  removeNodesDataMap.forEach(node => {
    const rNode = xmlValue.getElementsByTagName(node)[0];
    if (rNode) {
      rNode.parentNode.removeChild(rNode);
    }
  });
  const serializer = new     XMLSerializer().serializeToString(xmlValue.getElementById('root'));
  return serializer;
}

const input = `
<Source>ABC</Source>
<ProductCode>77</ProductCode>
<ProductCategory>Performance</ProductCategory>
<PublicationDateTime>2019-03-06  17:04:5.000</PublicationDateTime>
<ArticleID>123254</ArticleID>
<Author/>
<records>
<record>some record stuff that I actually want to return along with node</record>
</records>
`;

console.log(removeXmlNodes(input))