forgiving-xml-parser
Enligsh | 简体中文
An XML/HTML parser and serializer for JavaScript. Playground
Features
- Transform XML/HTML to JSON(carry code locationInfo or parse steps)
- Transform JSON back to XML
- Works with node packages, in browser(like browser such as Miniprogram)
- Various options are available to customize the transformation
- custom parsing behavior(souch as allow
node-name
is empty) - supported events
- custom node parser
Usage
npm i forgiving-xml-parser -S
yarn add forgiving-xml-parser
const ForgivingXmlParser = require('forgiving-xml-parser');
const json = ForgivingXmlParser.parse('...');
import {parse, serialize, ...} from 'forgiving-xml-parser';
const json = parse('...');
<script src="xxx/forgiving-xml-parser.js"></script>
<script>
const json = ForgivingXmlParser.parse("...");
</script>
const { parse, serialize, parseResultToJSON, FxParser } = require("forgiving-xml-parser");
const xml = `<p>hi xml</p>`;
const json = parseResultToJSON(parse(xml), {
allowAttrContentHasBr: true,
allowNodeNameEmpty: true,
allowNodeNotClose: true,
allowStartTagBoundaryNearSpace: true,
allowEndTagBoundaryNearSpace: true,
allowTagNameHasSpace: true,
allowNearAttrEqualSpace: true,
ignoreTagNameCaseEqual: false,
onEvent(type, context, data) {},
});
serialize(json);
const fxParser = new FxParser();
const json2 = parseResultToJSON(fxParser.parse(xml));
console.log(JSON.stringify(json2) === JSON.stringify(json));
console.log(fxParser.serialize(json2) === serialize(json));
Event trigger timing