NaturalIntelligence/fast-xml-parser

View on GitHub
docs/v5/3. Options.md

Summary

Maintainability
Test Coverage
# Configuration

There are 2 set of configurations.
- Output Builder specific options
- Parser specific options

Eg.

### Parser configuration

```js
{
  removeNSPrefix: false, // remove NS from tag name or attribute name if true
  //ignoreRootElement : false,
  stopNodes: [], //nested tags will not be parsed even for errors
  // isArray: () => false, //User will set it
  htmlEntities: false,
  // skipEmptyListItem: false
  tags:{
    unpaired: [],
    nameFor:{
      cdata: false,
      comment: false,
      text: '#text'
    },
    separateTextProperty: false,
  },
  attributes:{
    ignore: false,
    booleanType: true,
    entities: true,
  },

  // select: ["img[src]"],
  // stop: ["anim", "[ads]"]
  only: [], // rest tags will be skipped. It will result in flat array
  hierarchy: false, //will be used when a particular tag is set to be parsed.
  skip: [], // will be skipped from parse result. on('skip') will be triggered

  select: [], // on('select', tag => tag ) will be called if match
  stop: [], //given tagPath will not be parsed. innerXML will be set as string value
  OutputBuilder: new JsArrBuilder(),
}
```

**Note**: There would be some properties which are yet to be supported but mentioned for future use.

### Output Builder specific options

```js
{
  preserveOrder: false, // If set 'join' value parser will be used for tag values
  nameFor:{
    text: "#text",
    comment: "",
    cdata: "",
  },
  // onTagClose: () => {},
  // onAttribute: () => {},
  piTag: false,
  declaration: false, //"?xml"
  tags: {
    valueParsers: [
      // "trim",
      // "boolean",
      // "number",
      // "currency",
      // "date",
    ]
  },
  attributes:{
    prefix: "@_",
    suffix: "",
    groupBy: "",
    
    valueParsers: [
      // "trim",
      // "boolean",
      // "number",
      // "currency",
      // "date",
    ]
  }
}
```

By default `JsObjOutputBuilder` output builder is used with default options.  


Example

```js
const XMLParser = require("fast-xml-parse/v5/XMLParser");
const JsObjOutputBuilder = require("fast-xml-parse/v5/OutputBuilders/JsObjBuilder");
const JsArrBuilder = require("fast-xml-parse/v5/OutputBuilders/JsArrBuilder");
const JsMinArrBuilder = require("fast-xml-parse/v5/OutputBuilders/JsMinArrBuilder");

const xmlData = fs.readFileSync("sample.xml").toString();

const outputBuilderOptions = {
  onAttribute: (name, value, tagName) => {
    console.log(name, value, tagName)
  }
};

const parserOptions = {
  attributes: {
      ignore: false,
      booleanType:true
  },
  
  OutputBuilder: new JsObjOutputBuilder(outputBuilderOptions)
};

const parser = new XMLParser(parserOptions);
let result = parser.parse(xmlData);

console.log(JSON.stringify(result,null,4));
```