简体   繁体   中英

Parsing XML elements without closing tags in JavaScript

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM