UNPKG

4.34 kBMarkdownView Raw
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
8js2xmlparser is a Node.js module that parses JavaScript objects into XML.
9
10## Features
11
12Since XML is a data-interchange format, js2xmlparser is designed primarily for
13JSON-type objects, arrays and primitive data types, like many of the other
14JavaScript to XML parsers currently available for Node.js.
15
16However, js2xmlparser is capable of parsing any object, including native
17JavaScript objects such as `Date` and `RegExp`, by taking advantage of each
18object's `toString` function or, if this function does not exist, the `String`
19constructor.
20
21js2xmlparser also has support for the `Map` and `Set` objects introduced in
22ECMAScript 2015, treating them as JSON-type objects and arrays respectively.
23Support for `Map`s is necessary to generate XML with elements in a specific
24order, since JSON-type objects do not guarantee insertion order. `Map` keys are
25always converted to strings using the method described above.
26
27js2xmlparser 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
33js2xmlparser can also pretty-print the XML it outputs.
34
35## Installation
36
37The easiest way to install js2xmlparser is using npm:
38
39```
40npm install js2xmlparser
41```
42
43You can also build js2xmlparser from source using npm:
44
45```
46git clone https://github.com/michaelkourlas/node-js2xmlparser.git
47npm install
48npm run-script build
49```
50
51The `build` script will build the production variant of js2xmlparser, run all
52tests, and build the documentation.
53
54You can build the production variant without running tests using the script
55`prod`. You can also build the development version using the script `dev`.
56The only difference between the two is that the development version includes
57source maps.
58
59## Usage
60
61The documentation for the current version is available [here](http://www.kourlas.com/node-js2xmlparser/docs/4.0.2/).
62
63You can also build the documentation using npm:
64
65```
66npm run-script docs
67```
68
69## Examples
70
71The following example illustrates the basic usage of js2xmlparser:
72
73```javascript
74var js2xmlparser = require("js2xmlparser");
75
76var 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
115console.log(js2xmlparser.parse("person", obj));
116```
117
118This 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
139Additional examples can be found in the examples directory.
140
141## Tests
142
143js2xmlparser includes a set of tests to verify core functionality. You can run
144the tests using npm:
145
146```
147npm run-script test-prod
148```
149
150The only difference between the `test-prod` and `test-dev` scripts is that the
151development version includes source maps.
152
153## License
154
155js2xmlparser is licensed under the [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0).