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
+32-19Lines changed: 32 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
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
@@ -21,34 +21,47 @@ var xml = "<foo>bar</foo>";
21
21
var json = parser.toJson(xml); //returns a string containing the JSON structure by default
22
22
console.log(json);
23
23
```
24
-
### Options
24
+
##API
25
25
26
-
***object:** Returns a Javascript object instead of a JSON string
27
-
***reversible:** Makes the JSON reversible to XML (*)
28
-
***coerce:** Makes type coercion
29
-
***sanitize:** Sanitizes the following characters:
30
-
31
-
| Character | Escaped |
32
-
| --------- | ------- |
33
-
|`<`|`<`|
34
-
|`>`|`>`|
35
-
|`(`|`(`|
36
-
|||
37
-
|||
26
+
```
27
+
parser.toJson(xml, options);
28
+
```
29
+
```
30
+
parser.toXml(json, options);
31
+
```
38
32
39
-
***trim:**
33
+
### Options object
40
34
41
35
```
42
-
options = {
36
+
var options = {
43
37
object: false,
44
38
reversible: false,
45
39
coerce: true,
46
40
sanitize: true,
47
41
trim: true };
48
42
```
49
-
* if you want to get the Javascript object then you might want to invoke parser.toJson(xml, {object: true});
50
-
* if you want a reversible json to xml then you should use parser.toJson(xml, {reversible: true});
51
-
* if you want to keep xml space text then you should use `options.space`, `parser.toJson(xml, {space: true})`;
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
+
```
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