Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/imingyu/forgiving-xml-parser
An XML/HTML parser and serializer for JavaScript.
https://github.com/imingyu/forgiving-xml-parser
forgiving-xml-parser html html-parser html-parsing html2js html2json javascript json parser serializer transformation typescript xml xml-parser xml-parsing xml2js xml2json
Last synced: 3 months ago
JSON representation
An XML/HTML parser and serializer for JavaScript.
- Host: GitHub
- URL: https://github.com/imingyu/forgiving-xml-parser
- Owner: imingyu
- License: bsd-3-clause
- Created: 2020-09-23T07:15:33.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-09-26T10:22:08.000Z (over 1 year ago)
- Last Synced: 2024-11-03T12:06:26.223Z (3 months ago)
- Topics: forgiving-xml-parser, html, html-parser, html-parsing, html2js, html2json, javascript, json, parser, serializer, transformation, typescript, xml, xml-parser, xml-parsing, xml2js, xml2json
- Language: TypeScript
- Homepage: https://imingyu.github.io/forgiving-xml-parser/
- Size: 16.3 MB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# forgiving-xml-parser
[![Build Status](https://travis-ci.org/imingyu/forgiving-xml-parser.svg?branch=master)](https://travis-ci.org/imingyu/forgiving-xml-parser)
![image](https://img.shields.io/npm/l/forgiving-xml-parser.svg)
[![image](https://img.shields.io/npm/v/forgiving-xml-parser.svg)](https://www.npmjs.com/package/forgiving-xml-parser)
[![image](https://img.shields.io/npm/dt/forgiving-xml-parser.svg)](https://www.npmjs.com/package/forgiving-xml-parser)Enligsh | [简体中文](./README.zh-CN.md)
An XML/HTML parser and serializer for JavaScript. [Playground](https://imingyu.github.io/forgiving-xml-parser/)
![spec](./docs/img/ad.png)
# 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
- 1.install
```bash
# using npm
npm i forgiving-xml-parser -S
# using yarn
yarn add forgiving-xml-parser
```- 2.include
```javascript
// in node
const ForgivingXmlParser = require('forgiving-xml-parser');
const json = ForgivingXmlParser.parse('...');// in webpack
import {parse, serialize, ...} from 'forgiving-xml-parser';
const json = parse('...');
``````html
// global variable
const json = ForgivingXmlParser.parse("...");```
- 3.use
```javascript
const { parse, serialize, parseResultToJSON, FxParser } = require("forgiving-xml-parser");const xml = `
hi xml
`;
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) {},
}); // { "nodes": [{ "type": "element", "name": "p", "children": [{ "type": "text", "content": "hi xml" }] }] }serialize(json); //
hi xml
const fxParser = new FxParser();
const json2 = parseResultToJSON(fxParser.parse(xml));
console.log(JSON.stringify(json2) === JSON.stringify(json)); // true
console.log(fxParser.serialize(json2) === serialize(json)); // true
```Event trigger timing
![Legend](./docs/img/legend.png)