Parsing XML elements without closing tags in JavaScript

693 Views Asked by At

I am running into a problem with the fast-xml-parser npm package. I'm trying to parse some XML from an external source that has a series of elements with self closing tags but those elements have data in them that I need. Consider the following snippet:

const options = {
    unpairedTags: ["link"]
};
const parser = new XMLParser(options);
const obj = parser.parse('<link something="idc" data="i care about this data"/>');

You can see that I have tried adding an unpaired tag but obviously this doesn't work because the idc and data properties are not considered XML so obj still lands up being blank

1

There are 1 best solutions below

0
On BEST ANSWER

The default is to ignore attributes. Set the parse option ignoreAttributes

const options = {
  unpairedTags: ["link"],
  ignoreAttributes : false
};

https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/docs/v4/2.XMLparseOptions.md