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

Commit 5250374

Browse files
committed
Moar readme
1 parent 115e66f commit 5250374

1 file changed

Lines changed: 32 additions & 19 deletions

File tree

README.md

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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
@@ -21,34 +21,47 @@ var xml = "<foo>bar</foo>";
2121
var json = parser.toJson(xml); //returns a string containing the JSON structure by default
2222
console.log(json);
2323
```
24-
### Options
24+
## API
2525

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-
| `<` | `&lt;` |
34-
| `>` | `&gt;` |
35-
| `(` | `&#40;`|
36-
| | |
37-
| | |
26+
```
27+
parser.toJson(xml, options);
28+
```
29+
```
30+
parser.toXml(json, options);
31+
```
3832

39-
* **trim:**
33+
### Options object
4034

4135
```
42-
options = {
36+
var options = {
4337
object: false,
4438
reversible: false,
4539
coerce: true,
4640
sanitize: true,
4741
trim: true };
4842
```
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 = { '<': '&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.
5265

5366
## License
5467
(The MIT License)

0 commit comments

Comments
 (0)