Skip to content

Latest commit

 

History

History
98 lines (84 loc) · 1.75 KB

5. ValueParsers.md

File metadata and controls

98 lines (84 loc) · 1.75 KB

Value Parser

You can change the sequence of value parsers or remove one or provide your own parser to control the parsing.

By default following sequence of parsers is used to parse tag and attribute value

["trim","join", "number","boolean","currency"]

Entity and date parsers have to be set.

Example when you don't override default parsers

const JsObjOutputBuilder = require("fast-xml-parse/v5/OutputBuilders/JsObjBuilder");

const xmlData = `<root>
  <int>   1234    </int>
  <str>4567</str>
  <int>str 6789</int>
  <bool>true  </bool>
</root>`

const parser = new XMLParser({
    OutputBuilder: new JsObjOutputBuilder()
});
//read xmlData your own 
let result = parser.parse(xmlData, true);

Output

{
  "root": {
    "int": [
      1234,
      "str 6789"
    ],
    "str": 4567,
    "bool": true
  }
}

But if you override it then it will use sequence in the order you defined.

const JsObjOutputBuilder = require("fast-xml-parse/v5/OutputBuilders/JsObjBuilder");

const xmlData = `<root>
  <int>   1234    </int>
  <str>4567</str>
  <int>str 6789</int>
  <bool>true  </bool>
</root>`

const parser = new XMLParser({
    OutputBuilder: new JsObjOutputBuilder({
      tags: {
        valueParsers: ["number"]
      }
    })
});
//read xmlData your own 
let result = parser.parse(xmlData, true);

Output

{
    "root": {
        "int": [
            1234,
            "str 6789"
        ],
        "str": 4567,
        "bool": "true"
    }
}

FXP v5 also allow to set your own value parser. Eg;

const outputBuilderOptions = {
  tags: {
    valueParsers: [
      "trim",
      "boolean",
      new numberParser({
        hex: true,
        leadingZeros: true,
        eNotation: true
      }),
      "currency"
    ]
  }
}