javascriptnode.jsxmlxml-parsingfast-xml-parser

Get text nodes separately (in the correct order) with fast-xml-parser?


No matter what options I try, I seem to get all text nodes clustered together, without any information on where the inner XML nodes were inserted. It's easier to explain with a simple example:

<a>left <b>middle</b> right</a>

I expect this to give me something like this:

{
  tag: 'a',
  children: [
    'left ',
    { tag: 'b', children: ['middle'] },
    ' right',
  ]
}

The exact format doesn't matter, but middle is between left and right. The order of children elements is important to me. With fast-xml-parser, what I get instead is the following:

{
  "a": {
    "#text": "left  right",
    "b": "middle",
  }
}

I don't mind the different format, but I lost the information about the position of the <b>middle</b> node. From the JavaScript tree version of the XML file, there's no way to differentiate between these files, as they all parse into the same structure.

<a>left  right<b>middle</b></a>

<a><b>middle</b>left  right</a>

<a>left <b>middle</b> right</a>

<a>le<b>middle</b>ft  right</a>

Is there an option which will allow me to preserve the order of text nodes?


Solution

  • Unfortunately, it seems like the answer is that this option is simply not available in fast-xml-parser, as per this issue I found: https://github.com/NaturalIntelligence/fast-xml-parser/issues/175. Text nodes will get merged as one, and the team member explains that this is "not a bug but expected behavior".