You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`xmldoc` lets you parse XML documents with ease. It's a pure-JavaScript, one-file XML document class with a single dependency on the excellent [`sax`][sax] parser.
6
+
`xmldoc` lets you parse XML documents with ease. It's a lightweight XML document class with a single dependency on the excellent [`sax`][sax] parser.
8
7
9
8
For more on why I wrote this class, see the [blog post][blog].
I haven't tested this myself but [installing `buffer` and `stream` separately](https://github.com/nfarina/xmldoc/issues/38) may be necessary for `xmldoc` to work on React Native:
If you're using React Native, you may need to install `buffer` and `stream` separately:
54
+
55
+
```bash
56
+
npm install buffer stream xmldoc
57
+
```
58
+
41
59
## Classes
42
60
43
61
The primary exported class is `XmlDocument`, which you'll use to consume your XML text. `XmlDocument` contains a hierarchy of `XmlElement` instances representing the XML structure.
@@ -46,12 +64,12 @@ Both `XmlElement` and `XmlDocument` contain the same members and methods you can
46
64
47
65
## Members
48
66
49
-
*`name` - the node name, like "tat" for `<tat>`. XML "namespaces" are ignored by the underlying [sax-js](https://github.com/isaacs/sax-js) parser, so you'll simply get "office:body" for `<office:body>`.
50
-
*`attr` - an object dict containing attribute properties, like `bookNode.attr.title` for `<book title="...">`.
51
-
*`val` - the string "value" of the node, if any, like "world" for `<hello>world</hello>`.
52
-
*`children` - an array of `XmlElement` children of the node.
53
-
*`firstChild`, `lastChild` - pretty much what it sounds like; null if no children
54
-
*`line`, `column`, `position`, `startTagPosition` - information about the element's original position in the XML string.
67
+
-`name` - the node name, like "tat" for `<tat>`. XML "namespaces" are ignored by the underlying [sax-js](https://github.com/isaacs/sax-js) parser, so you'll simply get "office:body" for `<office:body>`.
68
+
-`attr` - an object dict containing attribute properties, like `bookNode.attr.title` for `<book title="...">`.
69
+
-`val` - the string "value" of the node, if any, like "world" for `<hello>world</hello>`.
70
+
-`children` - an array of `XmlElement` children of the node.
71
+
-`firstChild`, `lastChild` - pretty much what it sounds like; null if no children
72
+
-`line`, `column`, `position`, `startTagPosition` - information about the element's original position in the XML string.
55
73
56
74
Each member defaults to a sensible "empty" value like `{}` for `attr`, `[]` for `children`, and `""` for `val`.
57
75
@@ -122,16 +140,19 @@ This is just an override of the standard JavaScript method, it will give you a s
122
140
The default implementation of `toString()`, that is, the one you get when you just `console.log("Doc: " + myDoc)` will pretty-print the XML with linebreaks and indents. You can pass a couple options to control the output:
123
141
124
142
```js
125
-
xml.toString({compressed:true}) // strips indents and linebreaks
126
-
xml.toString({trimmed:true}) // trims long strings for easier debugging
127
-
xml.toString({preserveWhitespace:true}) // prevents whitespace being removed from around element values
143
+
xml.toString({ compressed:true }); // strips indents and linebreaks
144
+
xml.toString({ trimmed:true }); // trims long strings for easier debugging
145
+
xml.toString({ preserveWhitespace:true }); // prevents whitespace from being removed from around element values
146
+
xml.toString({ html:true }); // uses HTML self-closing tag rules for elements without children
128
147
```
129
148
130
149
Putting it all together:
131
150
132
151
```js
133
152
var xml ="<author><name>looooooong value</name></author>";
0 commit comments