node-xml2js icon indicating copy to clipboard operation
node-xml2js copied to clipboard

Empty string nodes

Open Marius8881 opened this issue 5 years ago • 1 comments

Is it possible to view empty string nodes, example name:" " , with tagNameProcessors, apparently I can not see them ?

Marius8881 avatar Feb 24 '20 06:02 Marius8881

It would probably be better to use attrValueProcessors if you want to access attribute. For example, , the following demo which uses attrValueProcessors can extract node1's name=""

    const root = `
        <xml>
            <node1 name="">node text</node1>
            <node2 name="node2">node2 text</node2>
        </xml>
    `;

    const tagNameProcessor = function (name) {
        console.log("node:", name);
        return name;
    }

    const attrValueProcessor = function (value, name) {
        console.log("name:", name, ",value:", value);
        return value;
    }

    const parser = xml2js.Parser({ attrValueProcessors: [attrValueProcessor], tagNameProcessors: [tagNameProcessor] });
    parser.parseString(root, function (err, result) {
        if (err) {
            console.error(err);
        }
        console.log(result);
    });

the output of attrValueProcessor are as followed:

name: name ,value: 
name: name ,value: node2

Omega-Ariston avatar Feb 26 '20 01:02 Omega-Ariston