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
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)
122
122
```js
123
123
var result =parser.validate(xmlData);
124
124
if(result !==true) cnosole.log(result.err);
125
125
var jsonObj =parser.parse(xmlData);
126
126
```
127
127
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 =newParser(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.
129
161
130
162
## Comparision
131
163
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
0 commit comments