1 | # js2xmlparser
|
2 |
|
3 | [![Node.js CI](https://github.com/michaelkourlas/node-js2xmlparser/actions/workflows/node.js.yml/badge.svg)](https://github.com/michaelkourlas/node-js2xmlparser/actions/workflows/node.js.yml)
|
4 | [![npm version](https://badge.fury.io/js/js2xmlparser.svg)](https://badge.fury.io/js/js2xmlparser)
|
5 |
|
6 | ## Overview
|
7 |
|
8 | js2xmlparser is a Node.js module that parses JavaScript objects into XML.
|
9 |
|
10 | ## Features
|
11 |
|
12 | Since XML is a data-interchange format, js2xmlparser is designed primarily for
|
13 | JSON-type objects, arrays and primitive data types, like many of the other
|
14 | JavaScript to XML parsers currently available for Node.js.
|
15 |
|
16 | However, js2xmlparser is capable of parsing any object, including native
|
17 | JavaScript objects such as `Date` and `RegExp`, by taking advantage of each
|
18 | object's `toString` function or, if this function does not exist, the `String`
|
19 | constructor.
|
20 |
|
21 | js2xmlparser also has support for the `Map` and `Set` objects introduced in
|
22 | ECMAScript 2015, treating them as JSON-type objects and arrays respectively.
|
23 | Support for `Map`s is necessary to generate XML with elements in a specific
|
24 | order, since JSON-type objects do not guarantee insertion order. `Map` keys are
|
25 | always converted to strings using the method described above.
|
26 |
|
27 | js2xmlparser also supports a number of constructs unique to XML:
|
28 |
|
29 | - attributes (through an attribute property in objects)
|
30 | - mixed content (through value properties in objects)
|
31 | - multiple elements with the same name (through arrays)
|
32 |
|
33 | js2xmlparser can also pretty-print the XML it outputs.
|
34 |
|
35 | ## Installation
|
36 |
|
37 | The easiest way to install js2xmlparser is using npm:
|
38 |
|
39 | ```
|
40 | npm install js2xmlparser
|
41 | ```
|
42 |
|
43 | You can also build js2xmlparser from source using npm:
|
44 |
|
45 | ```
|
46 | git clone https://github.com/michaelkourlas/node-js2xmlparser.git
|
47 | npm install
|
48 | npm run-script build
|
49 | ```
|
50 |
|
51 | The `build` script will build the production variant of js2xmlparser, run all
|
52 | tests, and build the documentation.
|
53 |
|
54 | You can build the production variant without running tests using the script
|
55 | `prod`. You can also build the development version using the script `dev`.
|
56 | The only difference between the two is that the development version includes
|
57 | source maps.
|
58 |
|
59 | ## Usage
|
60 |
|
61 | The documentation for the current version is available [here](http://www.kourlas.com/node-js2xmlparser/docs/5.0.0/).
|
62 |
|
63 | You can also build the documentation using npm:
|
64 |
|
65 | ```
|
66 | npm run-script docs
|
67 | ```
|
68 |
|
69 | ## Examples
|
70 |
|
71 | The following example illustrates the basic usage of js2xmlparser:
|
72 |
|
73 | ```javascript
|
74 | var js2xmlparser = require("js2xmlparser");
|
75 |
|
76 | var obj = {
|
77 | "@": {
|
78 | type: "natural",
|
79 | },
|
80 | firstName: "John",
|
81 | lastName: "Smith",
|
82 | dateOfBirth: new Date(1964, 7, 26),
|
83 | address: {
|
84 | "@": {
|
85 | type: "home",
|
86 | },
|
87 | streetAddress: "3212 22nd St",
|
88 | city: "Chicago",
|
89 | state: "Illinois",
|
90 | zip: 10000,
|
91 | },
|
92 | phone: [
|
93 | {
|
94 | "@": {
|
95 | type: "home",
|
96 | },
|
97 | "#": "123-555-4567",
|
98 | },
|
99 | {
|
100 | "@": {
|
101 | type: "cell",
|
102 | },
|
103 | "#": "890-555-1234",
|
104 | },
|
105 | {
|
106 | "@": {
|
107 | type: "work",
|
108 | },
|
109 | "#": "567-555-8901",
|
110 | },
|
111 | ],
|
112 | email: "john@smith.com",
|
113 | };
|
114 |
|
115 | console.log(js2xmlparser.parse("person", obj));
|
116 | ```
|
117 |
|
118 | This example produces the following XML:
|
119 |
|
120 | ```xml
|
121 | <?xml version='1.0'?>
|
122 | <person type='natural'>
|
123 | <firstName>John</firstName>
|
124 | <lastName>Smith</lastName>
|
125 | <dateOfBirth>Wed Aug 26 1964 00:00:00 GMT-0400 (Eastern Summer Time)</dateOfBirth>
|
126 | <address type='home'>
|
127 | <streetAddress>3212 22nd St</streetAddress>
|
128 | <city>Chicago</city>
|
129 | <state>Illinois</state>
|
130 | <zip>10000</zip>
|
131 | </address>
|
132 | <phone type='home'>123-555-4567</phone>
|
133 | <phone type='cell'>890-555-1234</phone>
|
134 | <phone type='work'>567-555-8901</phone>
|
135 | <email>john@smith.com</email>
|
136 | </person>
|
137 | ```
|
138 |
|
139 | Additional examples can be found in the examples directory.
|
140 |
|
141 | ## Tests
|
142 |
|
143 | js2xmlparser includes a set of tests to verify core functionality. You can run
|
144 | the tests using npm:
|
145 |
|
146 | ```
|
147 | npm run-script test-prod
|
148 | ```
|
149 |
|
150 | The only difference between the `test-prod` and `test-dev` scripts is that the
|
151 | development version includes source maps.
|
152 |
|
153 | ## License
|
154 |
|
155 | js2xmlparser is licensed under the [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0).
|