source: node_modules/xml/readme.md

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 4.7 KB
RevLine 
[d24f17c]1# xml [![Build Status](https://api.travis-ci.org/dylang/node-xml.svg)](http://travis-ci.org/dylang/node-xml)
2
3[![NPM](https://nodei.co/npm/xml.png?downloads=true)](https://nodei.co/npm/xml/)
4
5> Fast and simple Javascript-based XML generator/builder for Node projects.
6
7## Install
8
9 $ npm install xml
10
11## API
12
13### `xml(xmlObject, options)`
14
15Returns a `XML` string.
16
17```js
18var xml = require('xml');
19var xmlString = xml(xmlObject, options);
20```
21
22#### `xmlObject`
23
24`xmlObject` is a normal JavaScript Object/JSON object that defines the data for the XML string.
25
26Keys will become tag names.
27Values can be an `array of xmlObjects` or a value such as a `string` or `number`.
28
29```js
30xml({a: 1}) === '<a>1</a>'
31xml({nested: [{ keys: [{ fun: 'hi' }]}]}) === '<nested><keys><fun>hi</fun></keys></nested>'
32```
33
34There are two special keys:
35
36`_attr`
37
38Set attributes using a hash of key/value pairs.
39
40```js
41xml({a: [{ _attr: { attributes: 'are fun', too: '!' }}, 1]}) === '<a attributes="are fun" too="!">1</a>'
42````
43`_cdata`
44
45Value of `_cdata` is wrapped in xml `![CDATA[]]` so the data does not need to be escaped.
46
47```js
48xml({a: { _cdata: "i'm not escaped: <xml>!"}}) === '<a><![CDATA[i\'m not escaped: <xml>!]]></a>'
49```
50
51Mixed together:
52```js
53xml({a: { _attr: { attr:'hi'}, _cdata: "I'm not escaped" }}) === '<a attr="hi"><![CDATA[I\'m not escaped]]></a>'
54```
55
56#### `options`
57
58`indent` _optional_ **string** What to use as a tab. Defaults to no tabs (compressed).
59 For example you can use `'\t'` for tab character, or `' '` for two-space tabs.
60
61`stream` Return the result as a `stream`.
62
63**Stream Example**
64
65```js
66var elem = xml.element({ _attr: { decade: '80s', locale: 'US'} });
67var stream = xml({ toys: elem }, { stream: true });
68stream.on('data', function (chunk) {console.log("data:", chunk)});
69elem.push({ toy: 'Transformers' });
70elem.push({ toy: 'GI Joe' });
71elem.push({ toy: [{name:'He-man'}] });
72elem.close();
73
74/*
75result:
76data: <toys decade="80s" locale="US">
77data: <toy>Transformers</toy>
78data: <toy>GI Joe</toy>
79data: <toy>
80 <name>He-man</name>
81 </toy>
82data: </toys>
83*/
84```
85
86`Declaration` _optional_ Add default xml declaration as first node.
87
88_options_ are:
89* encoding: 'value'
90* standalone: 'value'
91
92**Declaration Example**
93
94```js
95xml([ { a: 'test' }], { declaration: true })
96//result: '<?xml version="1.0" encoding="UTF-8"?><a>test</a>'
97
98xml([ { a: 'test' }], { declaration: { standalone: 'yes', encoding: 'UTF-16' }})
99//result: '<?xml version="1.0" encoding="UTF-16" standalone="yes"?><a>test</a>'
100```
101
102## Examples
103
104**Simple Example**
105
106```js
107var example1 = [ { url: 'http://www.google.com/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower' } ];
108console.log(XML(example1));
109//<url>http://www.google.com/search?aq=f&amp;sourceid=chrome&amp;ie=UTF-8&amp;q=opower</url>
110```
111
112**Example with attributes**
113
114```js
115var example2 = [ { url: { _attr: { hostname: 'www.google.com', path: '/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower' } } } ];
116console.log(XML(example2));
117//result: <url hostname="www.google.com" path="/search?aq=f&amp;sourceid=chrome&amp;ie=UTF-8&amp;q=opower"/>
118```
119
120**Example with array of same-named elements and nice formatting**
121
122```js
123var example3 = [ { toys: [ { toy: 'Transformers' } , { toy: 'GI Joe' }, { toy: 'He-man' } ] } ];
124console.log(XML(example3));
125//result: <toys><toy>Transformers</toy><toy>GI Joe</toy><toy>He-man</toy></toys>
126console.log(XML(example3, true));
127/*
128result:
129<toys>
130 <toy>Transformers</toy>
131 <toy>GI Joe</toy>
132 <toy>He-man</toy>
133</toys>
134*/
135```
136
137**More complex example**
138
139```js
140var example4 = [ { toys: [ { _attr: { decade: '80s', locale: 'US'} }, { toy: 'Transformers' } , { toy: 'GI Joe' }, { toy: 'He-man' } ] } ];
141console.log(XML(example4, true));
142/*
143result:
144<toys decade="80s" locale="US">
145 <toy>Transformers</toy>
146 <toy>GI Joe</toy>
147 <toy>He-man</toy>
148</toys>
149*/
150```
151
152**Even more complex example**
153
154```js
155var example5 = [ { toys: [ { _attr: { decade: '80s', locale: 'US'} }, { toy: 'Transformers' } , { toy: [ { _attr: { knowing: 'half the battle' } }, 'GI Joe'] }, { toy: [ { name: 'He-man' }, { description: { _cdata: '<strong>Master of the Universe!</strong>'} } ] } ] } ];
156console.log(XML(example5, true));
157/*
158result:
159<toys decade="80s" locale="US">
160 <toy>Transformers</toy>
161 <toy knowing="half the battle">
162 GI Joe
163 </toy>
164 <toy>
165 <name>He-man</name>
166 <description><![CDATA[<strong>Master of the Universe!</strong>]]></description>
167 </toy>
168</toys>
169*/
170```
171
172## Tests
173
174Tests included use [AVA](https://ava.li). Use `npm test` to run the tests.
175
176 $ npm test
177
178## Examples
179
180There are examples in the examples directory.
181
182# Contributing
183
184Contributions to the project are welcome. Feel free to fork and improve. I accept pull requests when tests are included.
Note: See TracBrowser for help on using the repository browser.