Description
- Are you running the latest version? (v4.4.0)
- Have you included sample input, output, error, and expected output?
- Have you checked if you are using correct configuration?
- Did you try online tool? => yes but not possible to use it for this case
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?
- Yes
- No
Is it an expected behaviour of the tagValueProcessor
attribute on XMLParser?
Thanks the project 🙏