XMLParser uses XMLValidator on demand.
const {XMLParser} = require("fast-xml-parser");
try{
const parser = new XMLParser(options, true);
let result = parser.parse(XMLdata);
}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
};
};
Set it to true
when a tag can have boolean attributes.
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"]
});