You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 7, 2021. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+47-7Lines changed: 47 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,26 +2,66 @@
2
2
3
3
It does not parse the following elements:
4
4
5
-
* CDATA sections
5
+
* CDATA sections (*)
6
6
* Processing instructions
7
7
* XML declarations
8
8
* Entity declarations
9
9
* Comments
10
10
11
-
## Installation
12
-
`npm install xml2json`
11
+
## Installation
12
+
```
13
+
$ npm install xml2json
14
+
```
13
15
14
16
## Usage
15
17
```javascript
16
18
var parser =require('xml2json');
17
19
18
20
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
20
22
console.log(json);
21
23
```
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 = { '<':'<',
52
+
'>':'>',
53
+
'(':'(',
54
+
')':')',
55
+
'#':'#',
56
+
'&':'&',
57
+
'"':'"',
58
+
"'":''' };
59
+
```
60
+
61
+
62
+
63
+
64
+
(*) xml2json tranforms CDATA content to JSON, but it doesn't generate a reversible structure.
0 commit comments