4 Ever had the urge to parse XML? And wanted to access the data in some sane,
5 easy way? Don
't want to compile a C parser, for whatever reason? Then xml2js is
6 what you're looking
for!
11 Simple XML to JavaScript
object converter. It supports bi-directional conversion.
13 [xmlbuilder-js](https:
15 Note: If you
're looking for a full DOM parser, you probably want
16 [JSDom](https://github.com/tmpvar/jsdom).
21 Simplest way to install `xml2js` is to use [npm](http://npmjs.org), just `npm
22 install xml2js` which will download xml2js and all dependencies.
24 xml2js is also available via [Bower](http://bower.io/), just `bower install
25 xml2js` which will download xml2js and all dependencies.
30 No extensive tutorials required because you are a smart developer! The task of
31 parsing XML should be an easy one, so let's make it so! Here
's some examples.
33 Shoot-and-forget usage
34 ----------------------
36 You want to parse XML as simple and easy as possible? It's dangerous to go
40 var parseString = require(
'xml2js').parseString;
41 var xml =
"<root>Hello xml2js!</root>"
42 parseString(xml,
function (err, result) {
47 Can
't get easier than this, right? This works starting with `xml2js` 0.2.3.
48 With CoffeeScript it looks like this:
51 {parseString} = require 'xml2js
'
52 xml = "<root>Hello xml2js!</root>"
53 parseString xml, (err, result) ->
57 If you need some special options, fear not, `xml2js` supports a number of
58 options (see below), you can specify these as second argument:
61 parseString(xml, {trim: true}, function (err, result) {
68 That's right,
if you have been
using xml-simple or a home-grown
69 wrapper,
this was added in 0.1.11 just
for you:
72 var fs = require(
'fs'),
73 xml2js = require(
'xml2js');
75 var parser =
new xml2js.Parser();
76 fs.readFile(__dirname +
'/foo.xml',
function(err, data) {
77 parser.parseString(data,
function (err, result) {
84 Look ma, no
event listeners!
86 You can also use `xml2js` from
92 xml2js = require
'xml2js'
94 parser =
new xml2js.Parser()
95 fs.readFile __dirname +
'/foo.xml', (err, data) ->
96 parser.parseString data, (err, result) ->
101 But what happens
if you forget the `
new` keyword to create a
new `Parser`? In
102 the middle of a nightly coding session, it might
get lost, after all. Worry
103 not, we got you covered! Starting with 0.2.8 you can also leave it out, in
104 which
case `xml2js` will helpfully add it
for you, no bad surprises and
107 Parsing multiple files
108 ----------------------
110 If you want to parse multiple files, you have multiple possibilities:
112 * You can create one `xml2js.Parser` per file. That
's the recommended one
113 and is promised to always *just work*.
114 * You can call `reset()` on your parser object.
115 * You can hope everything goes well anyway. This behaviour is not
116 guaranteed work always, if ever. Use option #1 if possible. Thanks!
118 So you wanna some JSON?
119 -----------------------
121 Just wrap the `result` object in a call to `JSON.stringify` like this
122 `JSON.stringify(result)`. You get a string containing the JSON representation
123 of the parsed object that you can feed to JSON-hungry consumers.
128 You might wonder why, using `console.dir` or `console.log` the output at some
129 level is only `[Object]`. Don't worry,
this is not because `xml2js` got lazy.
130 That
's because Node uses `util.inspect` to convert the object into strings and
131 that function stops after `depth=2` which is a bit low for most XML.
133 To display the whole deal, you can use `console.log(util.inspect(result, false,
134 null))`, which displays the whole result.
136 So much for that, but what if you use
137 [eyes](https://github.com/cloudhead/eyes.js) for nice colored output and it
138 truncates the output with `…`? Don't fear, there
's also a solution for that,
139 you just need to increase the `maxLength` limit by creating a custom inspector
140 `var inspect = require('eyes
').inspector({maxLength: false})` and then you can
141 easily `inspect(result)`.
146 Since 0.4.0, objects can be also be used to build XML:
149 var fs = require('fs
'),
150 xml2js = require('xml2js
');
152 var obj = {name: "Super", Surname: "Man", age: 23};
154 var builder = new xml2js.Builder();
155 var xml = builder.buildObject(obj);
158 At the moment, a one to one bi-directional conversion is guaranteed only for
159 default configuration, except for `attrkey`, `charkey` and `explicitArray` options
160 you can redefine to your taste. Writing CDATA is supported via setting the `cdata`
163 Processing attribute, tag names and values
164 ------------------------------------------
166 Since 0.4.1 you can optionally provide the parser with attribute name and tag name processors as well as element value processors (Since 0.4.14, you can also optionally provide the parser with attribute value processors):
170 function nameToUpperCase(name){
171 return name.toUpperCase();
174 //transform all attribute and tag names and values to uppercase
176 tagNameProcessors: [nameToUpperCase],
177 attrNameProcessors: [nameToUpperCase],
178 valueProcessors: [nameToUpperCase],
179 attrValueProcessors: [nameToUpperCase]},
180 function (err, result) {
185 The `tagNameProcessors`, `attrNameProcessors`, `attrValueProcessors` and `valueProcessors` options
186 accept an `Array` of functions with the following signature:
190 //do something with `name`
195 Some processors are provided out-of-the-box and can be found in `lib/processors.js`:
197 - `normalize`: transforms the name to lowercase.
198 (Automatically used when `options.normalize` is set to `true`)
200 - `firstCharLowerCase`: transforms the first character to lower case.
201 E.g. 'MyTagName
' becomes 'myTagName
'
203 - `stripPrefix`: strips the xml namespace prefix. E.g `<foo:Bar/>` will become 'Bar
'.
204 (N.B.: the `xmlns` prefix is NOT stripped.)
206 - `parseNumbers`: parses integer-like strings as integers and float-like strings as floats
207 E.g. "0" becomes 0 and "15.56" becomes 15.56
209 - `parseBooleans`: parses boolean-like strings to booleans
210 E.g. "true" becomes true and "False" becomes false
215 Apart from the default settings, there are a number of options that can be
216 specified for the parser. Options are specified by ``new Parser({optionName:
217 value})``. Possible options are:
219 * `attrkey` (default: `$`): Prefix that is used to access the attributes.
220 Version 0.1 default was `@`.
221 * `charkey` (default: `_`): Prefix that is used to access the character
222 content. Version 0.1 default was `#`.
223 * `explicitCharkey` (default: `false`)
224 * `trim` (default: `false`): Trim the whitespace at the beginning and end of
226 * `normalizeTags` (default: `false`): Normalize all tag names to lowercase.
227 * `normalize` (default: `false`): Trim whitespaces inside text nodes.
228 * `explicitRoot` (default: `true`): Set this if you want to get the root
229 node in the resulting object.
230 * `emptyTag` (default: `''`): what will the value of empty nodes be.
231 * `explicitArray` (default: `true`): Always put child nodes in an array if
232 true; otherwise an array is created only if there is more than one.
233 * `ignoreAttrs` (default: `false`): Ignore all XML attributes and only create
235 * `mergeAttrs` (default: `false`): Merge attributes and child elements as
236 properties of the parent, instead of keying attributes off a child
237 attribute object. This option is ignored if `ignoreAttrs` is `false`.
238 * `validator` (default `null`): You can specify a callable that validates
239 the resulting structure somehow, however you want. See unit tests
241 * `xmlns` (default `false`): Give each element a field usually called '$ns
'
242 (the first character is the same as attrkey) that contains its local name
244 * `explicitChildren` (default `false`): Put child elements to separate
245 property. Doesn't work with `mergeAttrs =
true`. If element has no children
246 then
"children" won
't be created. Added in 0.2.5.
247 * `childkey` (default `$$`): Prefix that is used to access child elements if
248 `explicitChildren` is set to `true`. Added in 0.2.5.
249 * `preserveChildrenOrder` (default `false`): Modifies the behavior of
250 `explicitChildren` so that the value of the "children" property becomes an
251 ordered array. When this is `true`, every node will also get a `#name` field
252 whose value will correspond to the XML nodeName, so that you may iterate
253 the "children" array and still be able to determine node names. The named
254 (and potentially unordered) properties are also retained in this
255 configuration at the same level as the ordered "children" array. Added in
257 * `charsAsChildren` (default `false`): Determines whether chars should be
258 considered children if `explicitChildren` is on. Added in 0.2.5.
259 * `includeWhiteChars` (default `false`): Determines whether whitespace-only
260 text nodes should be included. Added in 0.4.17.
261 * `async` (default `false`): Should the callbacks be async? This *might* be
262 an incompatible change if your code depends on sync execution of callbacks.
263 Future versions of `xml2js` might change this default, so the recommendation
264 is to not depend on sync execution anyway. Added in 0.2.6.
265 * `strict` (default `true`): Set sax-js to strict or non-strict parsing mode.
266 Defaults to `true` which is *highly* recommended, since parsing HTML which
267 is not well-formed XML might yield just about anything. Added in 0.2.7.
268 * `attrNameProcessors` (default: `null`): Allows the addition of attribute
269 name processing functions. Accepts an `Array` of functions with following
273 //do something with `name`
278 * `attrValueProcessors` (default: `null`): Allows the addition of attribute
279 value processing functions. Accepts an `Array` of functions with following
283 //do something with `name`
288 * `tagNameProcessors` (default: `null`): Allows the addition of tag name
289 processing functions. Accepts an `Array` of functions with following
293 //do something with `name`
298 * `valueProcessors` (default: `null`): Allows the addition of element value
299 processing functions. Accepts an `Array` of functions with following
303 //do something with `name`
309 Options for the `Builder` class
310 -------------------------------
311 These options are specified by ``new Builder({optionName: value})``.
312 Possible options are:
314 * `rootName` (default `root` or the root key name): root element name to be used in case
315 `explicitRoot` is `false` or to override the root element name.
316 * `renderOpts` (default `{ 'pretty
': true, 'indent
': ' ', 'newline
': '\n
' }`):
317 Rendering options for xmlbuilder-js.
318 * pretty: prettify generated XML
319 * indent: whitespace for indentation (only when pretty)
320 * newline: newline char (only when pretty)
321 * `xmldec` (default `{ 'version
': '1.0
', 'encoding
': 'UTF-8
', 'standalone
': true }`:
322 XML declaration attributes.
323 * `xmldec.version` A version number string, e.g. 1.0
324 * `xmldec.encoding` Encoding declaration, e.g. UTF-8
325 * `xmldec.standalone` standalone document declaration: true or false
326 * `doctype` (default `null`): optional DTD. Eg. `{'ext
': 'hello.dtd
'}`
327 * `headless` (default: `false`): omit the XML header. Added in 0.4.3.
328 * `allowSurrogateChars` (default: `false`): allows using characters from the Unicode
330 * `cdata` (default: `false`): wrap text nodes in `<![CDATA[ ... ]]>` instead of
331 escaping when necessary. Does not add `<![CDATA[ ... ]]>` if it is not required.
334 `renderOpts`, `xmldec`,`doctype` and `headless` pass through to
335 [xmlbuilder-js](https://github.com/oozcitak/xmlbuilder-js).
337 Updating to new version
338 =======================
340 Version 0.2 changed the default parsing settings, but version 0.1.14 introduced
341 the default settings for version 0.2, so these settings can be tried before the
345 var xml2js = require('xml2js
');
346 var parser = new xml2js.Parser(xml2js.defaults["0.2"]);
349 To get the 0.1 defaults in version 0.2 you can just use
350 `xml2js.defaults["0.1"]` in the same place. This provides you with enough time
351 to migrate to the saner way of parsing in `xml2js` 0.2. We try to make the
352 migration as simple and gentle as possible, but some breakage cannot be
355 So, what exactly did change and why? In 0.2 we changed some defaults to parse
356 the XML in a more universal and sane way. So we disabled `normalize` and `trim`
357 so `xml2js` does not cut out any text content. You can reenable this at will of
358 course. A more important change is that we return the root tag in the resulting
359 JavaScript structure via the `explicitRoot` setting, so you need to access the
360 first element. This is useful for anybody who wants to know what the root node
361 is and preserves more information. The last major change was to enable
362 `explicitArray`, so everytime it is possible that one might embed more than one
363 sub-tag into a tag, xml2js >= 0.2 returns an array even if the array just
364 includes one element. This is useful when dealing with APIs that return
365 variable amounts of subtags.
367 Running tests, development
368 ==========================
370 [](https://travis-ci.org/Leonidas-from-XIV/node-xml2js)
371 [](https://coveralls.io/r/Leonidas-from-XIV/node-xml2js?branch=master)
372 [](https://david-dm.org/Leonidas-from-XIV/node-xml2js)
374 The development requirements are handled by npm, you just need to install them.
375 We also have a number of unit tests, they can be run using `npm test` directly
376 from the project root. This runs zap to discover all the tests and execute
379 If you like to contribute, keep in mind that `xml2js` is written in
380 CoffeeScript, so don't develop on the JavaScript files that are checked into
381 the repository
for convenience reasons. Also, please write some unit test to
382 check your behaviour and
if it is some user-facing thing, add some
383 documentation to
this README, so people will know it exists. Thanks in advance!
388 Please,
if you have a problem with the library, first make sure you read
this
389 README. If you read
this far, thanks, you
're good. Then, please make sure your
390 problem really is with `xml2js`. It is? Okay, then I'll look at it. Send me a
391 mail and we can talk. Please don
't open issues, as I don't think that is the
392 proper forum
for support problems. Some problems might as well really be bugs
393 in `xml2js`,
if so I
'll let you know to open an issue instead :)
395 But if you know you really found a bug, feel free to open an issue instead.