NaturalIntelligence / fast-xml-parser

Validate XML, Parse XML and Build XML rapidly without C/C++ based libraries and no callback.
https://naturalintelligence.github.io/fast-xml-parser/
MIT License
2.45k stars 296 forks source link

unpairedTags matching requires paired tag sibling to work #562

Closed nibk closed 1 year ago

nibk commented 1 year ago

Description

unpairedTags option doesn't work if not followed immediately by a paired tag, its parent consumes the next sibling.

Input

<root>
  <a>
    <unpaired>
  </a>
  <b>whatever</b>
</root>

Code

As per the API example:

const xmlDataStr = `
<root>
    <a>
        <unpaired>
    </a>
    <b>whatever</b>
</root>
`;

const options = {
  unpairedTags: ["unpaired"]
};
const parser = new XMLParser(options);
const output = parser.parse(xmlDataStr);

Output

{
        "root": {
                "a": {
                        "unpaired": "",
                        "b": "whatever"
                }
        }
}

Correct structure can be generated by changing input to:

<root>
  <a>
    <unpaired><pair>foo</pair>
  </a>
  <b>whatever</b>
</root>

which triggers correct parsing, eg:

{
        "root": {
                "a": {
                        "unpaired": "",
                        "pair": "foo"
                },
                "b": "whatever"
        }
}

expected data

{
        "root": {
                "a": {
                        "unpaired": "",
                },
                "b": "whatever"
        }
}

Would you like to work on this issue?

github-actions[bot] commented 1 year ago

I'm glad you find this repository helpful. I'll try to address your issue ASAP. You can watch the repo for new changes or star it.

amitguptagwl commented 1 year ago

This issue is fixed in v4.2.2, Please visit solothought to read about latest features released.