Skip to content

Latest commit

 

History

History
52 lines (41 loc) · 1.12 KB

4.XMLValidator.md

File metadata and controls

52 lines (41 loc) · 1.12 KB

XMLParser uses XMLValidator on demand.

const {XMLParser} = require("fast-xml-parser");
const parser = new XMLParser(options);
try{
    let result = parser.parse(XMLdata, true);
}catch(err){
    //:
}

XML Parser throws error when XML Validator returns error. XML Validator can also be used directly without XML Parser;

const {XMLValidator} = require("fast-xml-parser");
const result = XMLValidator.validate(xmlData, {
    allowBooleanAttributes: true
});
  • XMLValidator returns true if no issue is found.
  • XMLValidator returns an error object if any issue is found.
{
  err: {
    code: string;
    msg: string,
    line: number,
    col: number
  };
};

Options

allowBooleanAttributes

Set it to true when a tag can have boolean attributes.

unpairedTags

Unpaired Tags are the tags which don't have matching closing tag. Eg <br> in HTML. You can parse unpaired tags by providing their list to the parser, validator and builder.

const xmlData = `<parent><extra></parent>`;
const result = XMLValidator.validate( xmlData, {
  unpairedTags: ["extra"]
});

> Next: Entities