Я пытаюсь использовать node-htmlparser2 и застрял прямо в самом начале.У меня есть тысячи XML-файлов, например:
<document … loads of attribs …>
<foo … loads of attribs …>
<loads…> … </loads>
<of…> … </of>
<other…> … </other>
<tags…> … </tags>
</foo>
</document>
Я хочу, чтобы все внутри <foo></foo>
было одной строкой.Мой код ниже работает, но мне не кажется, что это правильный путь
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();
}
Есть ли лучший способ работать с htmlparser2 без этого глупого isFoo
флага?