Skip to content

Commit 5cfcd7e

Browse files
committed
j2x: update README
1 parent 2a245e9 commit 5cfcd7e

File tree

4 files changed

+59670
-2
lines changed

4 files changed

+59670
-2
lines changed

README.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,46 @@ $cat xmlfile.xml | xml2js [-ns|-a|-c] [-o outputfile.json]
118118

119119
To use it **on webpage**
120120

121-
1. Download and include [parser.js](https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/lib/parser.js)
121+
1. Download and include [parser.js] Or use directly from [CDN](https://cdnjs.com/libraries/fast-xml-parser)(https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/lib/parser.js)
122122
```js
123123
var result = parser.validate(xmlData);
124124
if(result !== true) cnosole.log(result.err);
125125
var jsonObj = parser.parse(xmlData);
126126
```
127127

128-
Or use directly from [CDN](https://cdnjs.com/libraries/fast-xml-parser)
128+
## JSON or JS Object to XML
129+
130+
```js
131+
var Parser = require("fast-xml-parser").j2xParser;
132+
//default options need not to set
133+
var defaultOptions = {
134+
attributeNamePrefix : "@_",
135+
attrNodeName: "@", //default is set to false
136+
textNodeName : "#text",
137+
ignoreAttributes : true,
138+
encodeHTMLchar: false,
139+
cdataTagName: "__cdata", //default is set to false
140+
cdataPositionChar: "\\c",
141+
format: false,
142+
indentBy: " ",
143+
supressEmptyNode: false
144+
};
145+
var parser = new Parser(defaultOptions);
146+
var xml = parser.parse(json_or_js_obj);
147+
148+
```
149+
**OPTIONS** :
150+
151+
152+
* **attributeNamePrefix** : Identify attributes with this prefix otherwise treat them as a tag.
153+
* **attrNodeName**: Identify attributes with this name when they are grouped under single property.
154+
* **ignoreAttributes** : Don't check for attributes. Treats everything as tag.
155+
* **encodeHTMLchar** : encodes values (except cdata values) when writing to XML.
156+
* **cdataTagName** : If specified, parse matching tag as CDATA
157+
* **cdataPositionChar** : Identify the position where CDATA tag should be placed. If it is blank then CDATA will be added in the last of tag's value.
158+
* **format** : If set to true, then format the XML output.
159+
* **indentBy** : indent by this char `when` format is set to `true`
160+
* **supressEmptyNode** : If set to `true`, tags with no value (text or nested tags) are written as self closing tags.
129161

130162
## Comparision
131163
I decided to created this library when I couldn't find any library which can convert XML data to json without any callback and which is not based on any C/C++ library.
@@ -158,6 +190,14 @@ Installation of such libraries fails on some OS. You may require to install miss
158190

159191
![npm_xml2json_compare](static/img/fxp-validatorv3.png)
160192

193+
#### Benchmark for JSON to XML
194+
195+
| file size | fxp 3.2 js to xml | xml2js 0.4.19 builder |
196+
|------------|-----------------|-----------------|
197+
| 1.3k | 160148.9801 | 10384.99401|
198+
| 1.1m | 173.6374831 | 8.611884025|
199+
200+
![npm_xml2json_compare](static/img/j2x.png)
161201
# Changes from v3
162202

163203
* It can handle big file now (I have tested up to 98mb). Performance report is given above.

0 commit comments

Comments
 (0)