Leonidas-from-XIV / node-xml2js

XML to JavaScript object converter.
MIT License
4.87k stars 601 forks source link

Empty string nodes #558

Open Marius8881 opened 4 years ago

Marius8881 commented 4 years ago

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

Omega-Ariston commented 4 years ago

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