Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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)