1 | # xml [](http://travis-ci.org/dylang/node-xml)
|
---|
2 |
|
---|
3 | [](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 |
|
---|
15 | Returns a `XML` string.
|
---|
16 |
|
---|
17 | ```js
|
---|
18 | var xml = require('xml');
|
---|
19 | var 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 |
|
---|
26 | Keys will become tag names.
|
---|
27 | Values can be an `array of xmlObjects` or a value such as a `string` or `number`.
|
---|
28 |
|
---|
29 | ```js
|
---|
30 | xml({a: 1}) === '<a>1</a>'
|
---|
31 | xml({nested: [{ keys: [{ fun: 'hi' }]}]}) === '<nested><keys><fun>hi</fun></keys></nested>'
|
---|
32 | ```
|
---|
33 |
|
---|
34 | There are two special keys:
|
---|
35 |
|
---|
36 | `_attr`
|
---|
37 |
|
---|
38 | Set attributes using a hash of key/value pairs.
|
---|
39 |
|
---|
40 | ```js
|
---|
41 | xml({a: [{ _attr: { attributes: 'are fun', too: '!' }}, 1]}) === '<a attributes="are fun" too="!">1</a>'
|
---|
42 | ````
|
---|
43 | `_cdata`
|
---|
44 |
|
---|
45 | Value of `_cdata` is wrapped in xml `![CDATA[]]` so the data does not need to be escaped.
|
---|
46 |
|
---|
47 | ```js
|
---|
48 | xml({a: { _cdata: "i'm not escaped: <xml>!"}}) === '<a><![CDATA[i\'m not escaped: <xml>!]]></a>'
|
---|
49 | ```
|
---|
50 |
|
---|
51 | Mixed together:
|
---|
52 | ```js
|
---|
53 | xml({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
|
---|
66 | var elem = xml.element({ _attr: { decade: '80s', locale: 'US'} });
|
---|
67 | var stream = xml({ toys: elem }, { stream: true });
|
---|
68 | stream.on('data', function (chunk) {console.log("data:", chunk)});
|
---|
69 | elem.push({ toy: 'Transformers' });
|
---|
70 | elem.push({ toy: 'GI Joe' });
|
---|
71 | elem.push({ toy: [{name:'He-man'}] });
|
---|
72 | elem.close();
|
---|
73 |
|
---|
74 | /*
|
---|
75 | result:
|
---|
76 | data: <toys decade="80s" locale="US">
|
---|
77 | data: <toy>Transformers</toy>
|
---|
78 | data: <toy>GI Joe</toy>
|
---|
79 | data: <toy>
|
---|
80 | <name>He-man</name>
|
---|
81 | </toy>
|
---|
82 | data: </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
|
---|
95 | xml([ { a: 'test' }], { declaration: true })
|
---|
96 | //result: '<?xml version="1.0" encoding="UTF-8"?><a>test</a>'
|
---|
97 |
|
---|
98 | xml([ { 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
|
---|
107 | var example1 = [ { url: 'http://www.google.com/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower' } ];
|
---|
108 | console.log(XML(example1));
|
---|
109 | //<url>http://www.google.com/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower</url>
|
---|
110 | ```
|
---|
111 |
|
---|
112 | **Example with attributes**
|
---|
113 |
|
---|
114 | ```js
|
---|
115 | var example2 = [ { url: { _attr: { hostname: 'www.google.com', path: '/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower' } } } ];
|
---|
116 | console.log(XML(example2));
|
---|
117 | //result: <url hostname="www.google.com" path="/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower"/>
|
---|
118 | ```
|
---|
119 |
|
---|
120 | **Example with array of same-named elements and nice formatting**
|
---|
121 |
|
---|
122 | ```js
|
---|
123 | var example3 = [ { toys: [ { toy: 'Transformers' } , { toy: 'GI Joe' }, { toy: 'He-man' } ] } ];
|
---|
124 | console.log(XML(example3));
|
---|
125 | //result: <toys><toy>Transformers</toy><toy>GI Joe</toy><toy>He-man</toy></toys>
|
---|
126 | console.log(XML(example3, true));
|
---|
127 | /*
|
---|
128 | result:
|
---|
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
|
---|
140 | var example4 = [ { toys: [ { _attr: { decade: '80s', locale: 'US'} }, { toy: 'Transformers' } , { toy: 'GI Joe' }, { toy: 'He-man' } ] } ];
|
---|
141 | console.log(XML(example4, true));
|
---|
142 | /*
|
---|
143 | result:
|
---|
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
|
---|
155 | var 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>'} } ] } ] } ];
|
---|
156 | console.log(XML(example5, true));
|
---|
157 | /*
|
---|
158 | result:
|
---|
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 |
|
---|
174 | Tests included use [AVA](https://ava.li). Use `npm test` to run the tests.
|
---|
175 |
|
---|
176 | $ npm test
|
---|
177 |
|
---|
178 | ## Examples
|
---|
179 |
|
---|
180 | There are examples in the examples directory.
|
---|
181 |
|
---|
182 | # Contributing
|
---|
183 |
|
---|
184 | Contributions to the project are welcome. Feel free to fork and improve. I accept pull requests when tests are included.
|
---|