PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/eyes.protractor/node_modules/eyes.sdk/node_modules/restler/node_modules/xml2js/README.md

https://gitlab.com/prashanth-sams/protractortesting
Markdown | 288 lines | 231 code | 57 blank | 0 comment | 0 complexity | 635d18e5d299c3da955dc30e39b54baf MD5 | raw file
  1. node-xml2js
  2. ===========
  3. Ever had the urge to parse XML? And wanted to access the data in some sane,
  4. easy way? Don't want to compile a C parser, for whatever reason? Then xml2js is
  5. what you're looking for!
  6. Description
  7. ===========
  8. Simple XML to JavaScript object converter. It supports bi-directional conversion.
  9. Uses [sax-js](https://github.com/isaacs/sax-js/) and
  10. [xmlbuilder-js](https://github.com/oozcitak/xmlbuilder-js/).
  11. Note: If you're looking for a full DOM parser, you probably want
  12. [JSDom](https://github.com/tmpvar/jsdom).
  13. Installation
  14. ============
  15. Simplest way to install `xml2js` is to use [npm](http://npmjs.org), just `npm
  16. install xml2js` which will download xml2js and all dependencies.
  17. Usage
  18. =====
  19. No extensive tutorials required because you are a smart developer! The task of
  20. parsing XML should be an easy one, so let's make it so! Here's some examples.
  21. Shoot-and-forget usage
  22. ----------------------
  23. You want to parse XML as simple and easy as possible? It's dangerous to go
  24. alone, take this:
  25. ```javascript
  26. var parseString = require('xml2js').parseString;
  27. var xml = "<root>Hello xml2js!</root>"
  28. parseString(xml, function (err, result) {
  29. console.dir(result);
  30. });
  31. ```
  32. Can't get easier than this, right? This works starting with `xml2js` 0.2.3.
  33. With CoffeeScript it looks like this:
  34. ```coffeescript
  35. {parseString} = require 'xml2js'
  36. xml = "<root>Hello xml2js!</root>"
  37. parseString xml, (err, result) ->
  38. console.dir result
  39. ```
  40. If you need some special options, fear not, `xml2js` supports a number of
  41. options (see below), you can specify these as second argument:
  42. ```javascript
  43. parseString(xml, {trim: true}, function (err, result) {
  44. });
  45. ```
  46. Simple as pie usage
  47. -------------------
  48. That's right, if you have been using xml-simple or a home-grown
  49. wrapper, this was added in 0.1.11 just for you:
  50. ```javascript
  51. var fs = require('fs'),
  52. xml2js = require('xml2js');
  53. var parser = new xml2js.Parser();
  54. fs.readFile(__dirname + '/foo.xml', function(err, data) {
  55. parser.parseString(data, function (err, result) {
  56. console.dir(result);
  57. console.log('Done');
  58. });
  59. });
  60. ```
  61. Look ma, no event listeners!
  62. You can also use `xml2js` from
  63. [CoffeeScript](http://jashkenas.github.com/coffee-script/), further reducing
  64. the clutter:
  65. ```coffeescript
  66. fs = require 'fs',
  67. xml2js = require 'xml2js'
  68. parser = new xml2js.Parser()
  69. fs.readFile __dirname + '/foo.xml', (err, data) ->
  70. parser.parseString data, (err, result) ->
  71. console.dir result
  72. console.log 'Done.'
  73. ```
  74. But what happens if you forget the `new` keyword to create a new `Parser`? In
  75. the middle of a nightly coding session, it might get lost, after all. Worry
  76. not, we got you covered! Starting with 0.2.8 you can also leave it out, in
  77. which case `xml2js` will helpfully add it for you, no bad surprises and
  78. inexplicable bugs!
  79. "Traditional" usage
  80. -------------------
  81. Alternatively you can still use the traditional `addListener` variant that was
  82. supported since forever:
  83. ```javascript
  84. var fs = require('fs'),
  85. xml2js = require('xml2js');
  86. var parser = new xml2js.Parser();
  87. parser.addListener('end', function(result) {
  88. console.dir(result);
  89. console.log('Done.');
  90. });
  91. fs.readFile(__dirname + '/foo.xml', function(err, data) {
  92. parser.parseString(data);
  93. });
  94. ```
  95. If you want to parse multiple files, you have multiple possibilites:
  96. * You can create one `xml2js.Parser` per file. That's the recommended one
  97. and is promised to always *just work*.
  98. * You can call `reset()` on your parser object.
  99. * You can hope everything goes well anyway. This behaviour is not
  100. guaranteed work always, if ever. Use option #1 if possible. Thanks!
  101. So you wanna some JSON?
  102. -----------------------
  103. Just wrap the `result` object in a call to `JSON.stringify` like this
  104. `JSON.stringify(result)`. You get a string containing the JSON representation
  105. of the parsed object that you can feed to JSON-hungry consumers.
  106. Displaying results
  107. ------------------
  108. You might wonder why, using `console.dir` or `console.log` the output at some
  109. level is only `[Object]`. Don't worry, this is not because xml2js got lazy.
  110. That's because Node uses `util.inspect` to convert the object into strings and
  111. that function stops after `depth=2` which is a bit low for most XML.
  112. To display the whole deal, you can use `console.log(util.inspect(result, false,
  113. null))`, which displays the whole result.
  114. So much for that, but what if you use
  115. [eyes](https://github.com/cloudhead/eyes.js) for nice colored output and it
  116. truncates the output with ``? Don't fear, there's also a solution for that,
  117. you just need to increase the `maxLength` limit by creating a custom inspector
  118. `var inspect = require('eyes').inspector({maxLength: false})` and then you can
  119. easily `inspect(result)`.
  120. XML builder usage
  121. -----------------
  122. Since 0.4.0, objects can be also be used to build XML:
  123. ```javascript
  124. var fs = require('fs'),
  125. xml2js = require('xml2js');
  126. var obj = {name: "Super", Surname: "Man", age: 23};
  127. var builder = new xml2js.Builder();
  128. var xml = builder.buildObject(obj);
  129. ```
  130. At the moment, a one to one bi-directional conversion is guaranteed only for
  131. default configuration, except for `attrkey`, `charkey` and `explicitArray` options
  132. you can redefine to your taste. Writing CDATA is not currently supported.
  133. Options
  134. =======
  135. Apart from the default settings, there are a number of options that can be
  136. specified for the parser. Options are specified by ``new Parser({optionName:
  137. value})``. Possible options are:
  138. * `attrkey` (default: `$`): Prefix that is used to access the attributes.
  139. Version 0.1 default was `@`.
  140. * `charkey` (default: `_`): Prefix that is used to access the character
  141. content. Version 0.1 default was `#`.
  142. * `explicitCharkey` (default: `false`)
  143. * `trim` (default: `false`): Trim the whitespace at the beginning and end of
  144. text nodes.
  145. * `normalizeTags` (default: `false`): Normalize all tag names to lowercase.
  146. * `normalize` (default: `false`): Trim whitespaces inside text nodes.
  147. * `explicitRoot` (default: `true`): Set this if you want to get the root
  148. node in the resulting object.
  149. * `emptyTag` (default: `undefined`): what will the value of empty nodes be.
  150. Default is `{}`.
  151. * `explicitArray` (default: `true`): Always put child nodes in an array if
  152. true; otherwise an array is created only if there is more than one.
  153. * `ignoreAttrs` (default: `false`): Ignore all XML attributes and only create
  154. text nodes.
  155. * `mergeAttrs` (default: `false`): Merge attributes and child elements as
  156. properties of the parent, instead of keying attributes off a child
  157. attribute object. This option is ignored if `ignoreAttrs` is `false`.
  158. * `validator` (default `null`): You can specify a callable that validates
  159. the resulting structure somehow, however you want. See unit tests
  160. for an example.
  161. * `xmlns` (default `false`): Give each element a field usually called '$ns'
  162. (the first character is the same as attrkey) that contains its local name
  163. and namespace URI.
  164. * `explicitChildren` (default `false`): Put child elements to separate
  165. property. Doesn't work with `mergeAttrs = true`. If element has no children
  166. then "children" won't be created. Added in 0.2.5.
  167. * `childkey` (default `$$`): Prefix that is used to access child elements if
  168. `explicitChildren` is set to `true`. Added in 0.2.5.
  169. * `charsAsChildren` (default `false`): Determines whether chars should be
  170. considered children if `explicitChildren` is on. Added in 0.2.5.
  171. * `async` (default `false`): Should the callbacks be async? This *might* be
  172. an incompatible change if your code depends on sync execution of callbacks.
  173. xml2js 0.3 might change this default, so the recommendation is to not
  174. depend on sync execution anyway. Added in 0.2.6.
  175. * `strict` (default `true`): Set sax-js to strict or non-strict parsing mode.
  176. Defaults to `true` which is *highly* recommended, since parsing HTML which
  177. is not well-formed XML might yield just about anything. Added in 0.2.7.
  178. Options for the `Builder` class
  179. -------------------------------
  180. * `rootName` (default `root`): root element name to be used in case
  181. `explicitiRoot` is `false` or to override the root element name.
  182. * `renderOpts` (default `{ 'pretty': true, 'indent': ' ', 'newline': '\n' }`):
  183. Rendering options for xmlbuilder-js.
  184. * pretty: prettify generated XML
  185. * indent: whitespace for indentation (only when pretty)
  186. * newline: newline char (only when pretty)
  187. * `xmldec` (default `{ 'version': '1.0', 'encoding': 'UTF-8', 'standalone': true }`:
  188. XML declaration attributes.
  189. * `xmldec.version` A version number string, e.g. 1.0
  190. * `xmldec.encoding` Encoding declaration, e.g. UTF-8
  191. * `xmldec.standalone` standalone document declaration: true or false
  192. * `doctype` (default `null`): optional DTD. Eg. `{'ext': 'hello.dtd'}`
  193. renderOpts, xmldec and doctype pass through to [xmlbuilder-js](https://github.com/oozcitak/xmlbuilder-js)
  194. Updating to new version
  195. =======================
  196. Version 0.2 changed the default parsing settings, but version 0.1.14 introduced
  197. the default settings for version 0.2, so these settings can be tried before the
  198. migration.
  199. ```javascript
  200. var xml2js = require('xml2js');
  201. var parser = new xml2js.Parser(xml2js.defaults["0.2"]);
  202. ```
  203. To get the 0.1 defaults in version 0.2 you can just use
  204. `xml2js.defaults["0.1"]` in the same place. This provides you with enough time
  205. to migrate to the saner way of parsing in xml2js 0.2. We try to make the
  206. migration as simple and gentle as possible, but some breakage cannot be
  207. avoided.
  208. So, what exactly did change and why? In 0.2 we changed some defaults to parse
  209. the XML in a more universal and sane way. So we disabled `normalize` and `trim`
  210. so xml2js does not cut out any text content. You can reenable this at will of
  211. course. A more important change is that we return the root tag in the resulting
  212. JavaScript structure via the `explicitRoot` setting, so you need to access the
  213. first element. This is useful for anybody who wants to know what the root node
  214. is and preserves more information. The last major change was to enable
  215. `explicitArray`, so everytime it is possible that one might embed more than one
  216. sub-tag into a tag, xml2js >= 0.2 returns an array even if the array just
  217. includes one element. This is useful when dealing with APIs that return
  218. variable amounts of subtags.
  219. Running tests, development
  220. ==========================
  221. [![Build Status](https://secure.travis-ci.org/Leonidas-from-XIV/node-xml2js.png?branch=master)](https://travis-ci.org/Leonidas-from-XIV/node-xml2js)
  222. [![Dependency Status](https://david-dm.org/Leonidas-from-XIV/node-xml2js.png)](https://david-dm.org/Leonidas-from-XIV/node-xml2js)
  223. The development requirements are handled by npm, you just need to install them.
  224. We also have a number of unit tests, they can be run using `npm test` directly
  225. from the project root. This runs zap to discover all the tests and execute
  226. them.
  227. If you like to contribute, keep in mind that xml2js is written in CoffeeScript,
  228. so don't develop on the JavaScript files that are checked into the repository
  229. for convenience reasons. Also, please write some unit test to check your
  230. behaviour and if it is some user-facing thing, add some documentation to this
  231. README, so people will know it exists. Thanks in advance!