【问题标题】:Preserving HTML content when parsing XML using nodejs使用 nodejs 解析 XML 时保留 HTML 内容
【发布时间】:2021-01-24 19:58:10
【问题描述】:

我正在尝试解析第三方交付的在其中一个节点中包含 html 的 XML 文件,并避免解析 html(不幸的是,它没有包含在 CDATA 中)。例如:

<?xml version="1.0" encoding="iso-8859-1"?>
<News>
<Keywords>[..]</Keywords>
<Title>[..]</title>
<Body>
<Body.Content>[BODY_CONTENT_IN_HTML]</Body.Content>
</Body>
</News
</xml>

尝试将正文内容保留为 html 字符串。我尝试了 xml2js,但它解析了 html,如果我尝试为该节点反转它,我不会得到相同的 html。有谁知道我如何实现这一点(解析 xml 对象的其余部分,但将 body.content 节点的内容检索为文本)?谢谢。

【问题讨论】:

    标签: html node.js xml cdata


    【解决方案1】:

    您可以尝试将 xml 视为字符串并将其拆分,直到您提取 html。

    例如:

    let str = `<?xml version="1.0" encoding="iso-8859-1"?>
    <News>
    <Keywords>[..]</Keywords>
    <Title>[..]</title>
    <Body>
    <Body.Content><html><body>Hello, world!</body></html></Body.Content>
    </Body>
    </News
    </xml>
    `;
    let target = str.split('Content>')[1].split('</Body')[0];
    console.log(target);
    

    输出:

    <html><body>Hello, world!</body></html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-20
      • 1970-01-01
      • 2011-03-24
      • 2013-03-28
      相关资源
      最近更新 更多