Description
Hi, I think I found a bug in how tagValueProcessor is executed in XMLParser class. It seems to be called only on leaf nodes (see sample code below).
I searched for similar issues but none seem related, however there is a similar (closed) one for XMLBuilder:
Code
// test.js
const { XMLParser } = require('fast-xml-parser');
function customTagValueProcessor(tagName, tagValue, propertyPath, hasAttributes, isLeafNode) {
console.log('customTagValueProcessor() params:', {
tagName, tagValue, propertyPath, hasAttributes, isLeafNode,
})
return tagValue;
}
function parseData(data) {
const parser = new XMLParser({
ignoreAttributes: false,
parseAttributeValue: true,
allowBooleanAttributes: true,
attributeNamePrefix: '',
attributesGroupName: '#attributes',
textNodeName: '#value',
parseTagValue: true,
tagValueProcessor: customTagValueProcessor,
});
return parser.parse(data);
}
function main() {
const data = `
<?xml version="1.0" encoding="utf-8"?>
<foo attr1="val1" attr2="val2">
<bar attr3="val3" attr4="val4">
<baz attr5="val5.1" attr6="val6.1">some text value</baz>
<baz attr5="val5.2" attr6="val6.2">some other text value</baz>
</bar>
</foo>
`;
const parsedData = parseData(data);
console.log(JSON.stringify(parsedData, undefined, 2));
}
main();
then run node test.js in a terminal
Output
You should see only 2 logs for the customTagValueProcessor function:
customTagValueProcessor() params: {
tagName: 'baz',
tagValue: 'some text value',
propertyPath: 'foo.bar.baz',
hasAttributes: true,
isLeafNode: true
}
customTagValueProcessor() params: {
tagName: 'baz',
tagValue: 'some other text value',
propertyPath: 'foo.bar.baz',
hasAttributes: true,
isLeafNode: true
}
And the parsed data (which is properly parsed in this case)
Expected data
As said previously, the data is properly parsed, however I would expect that the customTagValueProcessor function would be called on any node (and not only leaf nodes), and the output should be something like (probably not in this order):
customTagValueProcessor() params: {
tagName: 'baz',
tagValue: 'some text value',
propertyPath: 'foo.bar.baz',
hasAttributes: true,
isLeafNode: true
}
customTagValueProcessor() params: {
tagName: 'baz',
tagValue: 'some other text value',
propertyPath: 'foo.bar.baz',
hasAttributes: true,
isLeafNode: true
}
customTagValueProcessor() params: {
tagName: 'bar',
tagValue: undefined,
propertyPath: 'foo.bar',
hasAttributes: true,
isLeafNode: false
}
customTagValueProcessor() params: {
tagName: 'foo',
tagValue: undefined,
propertyPath: 'foo',
hasAttributes: true,
isLeafNode: false
}
Would you like to work on this issue?
Is it an expected behaviour of the tagValueProcessor attribute on XMLParser?
Thanks the project 🙏
Description
Hi, I think I found a bug in how
tagValueProcessoris executed inXMLParserclass. It seems to be called only on leaf nodes (see sample code below).I searched for similar issues but none seem related, however there is a similar (closed) one for XMLBuilder:
Code
then run
node test.jsin a terminalOutput
You should see only 2 logs for the
customTagValueProcessorfunction:And the parsed data (which is properly parsed in this case)
Expected data
As said previously, the data is properly parsed, however I would expect that the
customTagValueProcessorfunction would be called on any node (and not only leaf nodes), and the output should be something like (probably not in this order):Would you like to work on this issue?
Is it an expected behaviour of the
tagValueProcessorattribute on XMLParser?Thanks the project 🙏