【问题标题】:parsing xml to extract text of a specific tag using htmlparser2使用 htmlparser2 解析 xml 以提取特定标签的文本
【发布时间】:2019-01-30 20:14:21
【问题描述】:

我正在尝试 node-htmlparser2,但一开始就被卡住了。我有成千上万个这样的 xml 文件:

<document … loads of attribs …>
    <foo … loads of attribs …>
        <loads…> … </loads>
        <of…> … </of>
        <other…> … </other>
        <tags…> … </tags>
    </foo>
</document>

我希望 &lt;foo&gt;&lt;/foo&gt; 中的所有内容都作为单个字符串。我下面的代码有效,但在我看来这不是正确的方法

let isFoo = false;
let txt = '';
const p = new htmlparser.Parser({
    onopentag: function(name, attribs){
        if (name === 'foo') {
            isFoo = true;
        }
    },
    ontext: function(text){
        if (isFoo) {
            txt += text;
        }
    },
    onclosetag: function(tagname){
        if (tagname === 'foo') {
            isFoo = false;
            return txt;
        }
    }
}, {decodeEntities: true, xmlMode: true});

let data = [];
for (let file in files) {
    let record = {
        filename: file,
        filetext: p.write(file)
    }
    data.push(record);
    p.end();
}

没有那个愚蠢的isFoo标志,有没有更好的方法来使用htmlparser2?

【问题讨论】:

    标签: node.js xml html-parser


    【解决方案1】:

    这是一种可能的方法,灵感来自DomHandler's NPM page 上给出的示例,以及h.DomUtils 上一个丑陋的console.log

    const h = require('htmlparser2');
    const fs = require('fs');
    const data = []; // your output
    
    files.map((file) => { // files is assumed to be populated
      const record = {
        filename: file
      };
      data.push(record);
      const dh = new h.DomHandler((err, dom) => {
        if (err) return record.err = err;
        // DomUtils has many useful methods, most of them you know already, pick your preferred
        const e = h.DomUtils.getElementsByTagName('foo', dom)[0];
        // getText: only text nodes, getInnerHTML: everything, inner tags included
        record.filetext = h.DomUtils.getText(e);
      });
      const parser = new h.Parser(dh, {decodeEntities: true, xmlMode: true});
      fs.readFile(file, (err, content) => {
        if (err) return record.err = err;
        parser.write(content);
        parser.end();
      });
    });
    
    

    【讨论】:

      猜你喜欢
      • 2016-11-26
      • 1970-01-01
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-02
      • 2016-06-10
      • 1970-01-01
      相关资源
      最近更新 更多