Skip to content
This repository was archived by the owner on Jan 7, 2021. It is now read-only.

Commit 8379a54

Browse files
committed
Merge pull request #29 from buglabs/html-sanitizer
Updates README
2 parents 70e6d46 + ef331c4 commit 8379a54

2 files changed

Lines changed: 48 additions & 7 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
*.swp
3+
*.DS_Store

README.md

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,66 @@
22

33
It does not parse the following elements:
44

5-
* CDATA sections
5+
* CDATA sections (*)
66
* Processing instructions
77
* XML declarations
88
* Entity declarations
99
* Comments
1010

11-
## Installation
12-
`npm install xml2json`
11+
## Installation
12+
```
13+
$ npm install xml2json
14+
```
1315

1416
## Usage
1517
```javascript
1618
var parser = require('xml2json');
1719

1820
var xml = "<foo>bar</foo>";
19-
var json = parser.toJson(xml); //returns an string containing the json structure by default
21+
var json = parser.toJson(xml); //returns a string containing the JSON structure by default
2022
console.log(json);
2123
```
22-
* if you want to get the Javascript object then you might want to invoke parser.toJson(xml, {object: true});
23-
* if you want a reversible json to xml then you should use parser.toJson(xml, {reversible: true});
24-
* if you want to keep xml space text then you should use `options.space`, `parser.toJson(xml, {space: true})`;
24+
## API
25+
26+
```javascript
27+
parser.toJson(xml, options);
28+
```
29+
```javascript
30+
parser.toXml(json, options);
31+
```
32+
33+
### Options object
34+
35+
```javascript
36+
var options = {
37+
object: false,
38+
reversible: false,
39+
coerce: true,
40+
sanitize: true,
41+
trim: true };
42+
```
43+
44+
* **object:** Returns a Javascript object instead of a JSON string
45+
* **reversible:** Makes the JSON reversible to XML (*)
46+
* **coerce:** Makes type coercion. i.e.: numbers and booleans present in attributes and element values are converted from string to its correspondent data types.
47+
* **trim:** Removes leading and trailing whitespaces as well as line terminators in element values.
48+
* **sanitize:** Sanitizes the following characters:
49+
50+
```javascript
51+
var chars = { '<': '&lt;',
52+
'>': '&gt;',
53+
'(': '&#40;',
54+
')': '&#41;',
55+
'#': '&#35;',
56+
'&': '&amp;',
57+
'"': '&quot;',
58+
"'": '&apos;' };
59+
```
60+
61+
62+
63+
64+
(*) xml2json tranforms CDATA content to JSON, but it doesn't generate a reversible structure.
2565

2666
## License
2767
(The MIT License)

0 commit comments

Comments
 (0)