Leonidas-from-XIV / node-xml2js

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

Better validation of attributes and char content when writting XML #566

Open jhorbulyk opened 4 years ago

jhorbulyk commented 4 years ago

Consider the following code:

const xml2js = require('xml2js');
const builder = new xml2js.Builder({
    headless: true,
});
sampleObject1 = {
    "root": {
        "$": {
            "foo": {"bar": "baz"}
        }
    }
}
console.log(`Sample A: \n${builder.buildObject(sampleObject1)}\n`);

sampleObject2 = {
    "root": {"_": {"foo":"bar"}}
}
console.log(`Sample B: \n${builder.buildObject(sampleObject2)}\n`);

Observe that the output is

Sample A: 
<root foo="[object Object]"/>

Sample B: 
<root>[object Object]</root>

In both cases, the input isn't logical. Any value supplied to attrKey at any level should be a simple object with out nested arrays or sub-objects. Likewise all charkey values should be a string. That isn't the case with these two examples which is why they produce strange outputs. However, ideally, there should be some sort of validation to check for these cases and produce errors in these cases.