xml-toolkit 0.0.6 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -1
- package/index.js +2 -1
- package/lib/AttributesMap.js +1 -1
- package/lib/MoxyLikeJsonEncoder.js +44 -20
- package/lib/SAXEvent.js +18 -0
- package/lib/XMLNode.js +9 -3
- package/lib/XMLReader.js +1 -1
- package/package.json +1 -1
- package/test/soap.xml +1 -1
- package/test/test.js +15 -15
package/README.md
CHANGED
|
@@ -1,4 +1,57 @@
|
|
|
1
1
|
# node-xml-toolkit
|
|
2
2
|
Collection of classes for dealing with XML
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
# Installation
|
|
5
|
+
|
|
6
|
+
```
|
|
7
|
+
npm i xml-toolkit
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
# Using
|
|
11
|
+
* [Reading a Record List](https://github.com/do-/node-xml-toolkit/wiki/Use-Case:-Reading-a-Record-List)
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
const {XMLReader, XMLNode} = require ('xml-toolkit')
|
|
15
|
+
|
|
16
|
+
const records = new XMLReader ({
|
|
17
|
+
filterElements : 'Record',
|
|
18
|
+
map : XMLNode.toObject ({})
|
|
19
|
+
}).process (xmlSource)
|
|
20
|
+
|
|
21
|
+
// ...then:
|
|
22
|
+
// await someLoader.load (records)
|
|
23
|
+
|
|
24
|
+
// ...or
|
|
25
|
+
// for await (const record of records) { // pull parser mode
|
|
26
|
+
|
|
27
|
+
// ...or
|
|
28
|
+
// records.on ('error', e => console.log (e))
|
|
29
|
+
// records.pipe (nextStream)
|
|
30
|
+
|
|
31
|
+
// ...or
|
|
32
|
+
// records.on ('error', e => console.log (e))
|
|
33
|
+
// records.on ('data', record => doSomethingWith (record))
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
* [Getting a Single Element](https://github.com/do-/node-xml-toolkit/wiki/Use-Case:-Getting-a-Single-Element)
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
const {XMLReader, XMLNode} = require ('xml-toolkit')
|
|
40
|
+
|
|
41
|
+
const data = await new XMLReader ({
|
|
42
|
+
filterElements : 'MyElementName',
|
|
43
|
+
map : XMLNode.toObject ({})
|
|
44
|
+
}).process (xmlSource).findFirst ()
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
* [Patching XML](https://github.com/do-/node-xml-toolkit/wiki/Use-Case:-Patching-XML)
|
|
48
|
+
|
|
49
|
+
```js
|
|
50
|
+
const {XMLReader} = require ('xml-toolkit')
|
|
51
|
+
|
|
52
|
+
let xmlResult = ''; for await (const node of new XMLReader ().process (xmlSource)) xmlResult +=
|
|
53
|
+
node.isCharacters && node.parent.localName === 'ThePlaceHolder' ? id :
|
|
54
|
+
node.xml
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
More information available in [wiki docs](https://github.com/do-/node-xml-toolkit/wiki).
|
package/index.js
CHANGED
|
@@ -3,5 +3,6 @@ const XMLReader = require ('./lib/XMLReader')
|
|
|
3
3
|
const SAXEvent = require ('./lib/SAXEvent')
|
|
4
4
|
const AttributesMap = require ('./lib/AttributesMap')
|
|
5
5
|
const MoxyLikeJsonEncoder = require ('./lib/MoxyLikeJsonEncoder')
|
|
6
|
+
const XMLNode = require ('./lib/XMLNode')
|
|
6
7
|
|
|
7
|
-
module.exports = {XMLLexer, XMLReader, SAXEvent, AttributesMap, MoxyLikeJsonEncoder}
|
|
8
|
+
module.exports = {XMLLexer, XMLReader, SAXEvent, AttributesMap, MoxyLikeJsonEncoder, XMLNode}
|
package/lib/AttributesMap.js
CHANGED
|
@@ -1,39 +1,63 @@
|
|
|
1
|
-
const SAXEvent = require ('./SAXEvent.js')
|
|
1
|
+
const SAXEvent = require ('./SAXEvent.js'), {CHARACTERS, END_ELEMENT} = SAXEvent.TYPES
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const GET_LOCAL_NAME = (localName, namespaceURI) => localName
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
const MoxyLikeJsonEncoder = function (options = {}) {
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
o [k].push (nv)
|
|
7
|
+
let {getName} = options; if (!getName) getName = GET_LOCAL_NAME
|
|
10
8
|
|
|
11
|
-
}
|
|
9
|
+
const xform = ({children, attributes}) => {
|
|
12
10
|
|
|
13
|
-
|
|
11
|
+
let o = null
|
|
14
12
|
|
|
15
|
-
|
|
13
|
+
const set = (nv, localName, namespaceURI) => {
|
|
14
|
+
|
|
15
|
+
const k = getName (localName, namespaceURI)
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
if (o === null) return o = {[k]: nv}
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
if (!(k in o)) return o [k] = nv
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
const ov = o [k]; if (!Array.isArray (ov)) return o [k] = [ov, nv]
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return o
|
|
23
|
+
ov.push (nv)
|
|
26
24
|
|
|
27
|
-
}
|
|
25
|
+
}
|
|
28
26
|
|
|
29
|
-
|
|
27
|
+
if (attributes != null)
|
|
28
|
+
|
|
29
|
+
for (const [name, value] of attributes.entries ())
|
|
30
|
+
|
|
31
|
+
set (value, attributes.getLocalName (name), attributes.getNamespaceURI (name))
|
|
32
|
+
|
|
33
|
+
if (children != null) for (const child of children) switch (child.type) {
|
|
34
|
+
|
|
35
|
+
case CHARACTERS:
|
|
36
|
+
|
|
37
|
+
return child.text
|
|
38
|
+
|
|
39
|
+
case END_ELEMENT:
|
|
40
|
+
|
|
41
|
+
set (xform (child), child.localName, child.namespaceURI)
|
|
42
|
+
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return o
|
|
46
|
+
|
|
47
|
+
}
|
|
30
48
|
|
|
31
49
|
return function (node) {
|
|
32
50
|
|
|
33
51
|
let result = xform (node)
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
52
|
+
|
|
53
|
+
const {wrap, map} = options
|
|
54
|
+
|
|
55
|
+
if (wrap) result = {[getName (node.localName, node.namespaceURI)]: result}
|
|
56
|
+
|
|
57
|
+
if (map) result = map (result)
|
|
58
|
+
|
|
59
|
+
return result
|
|
60
|
+
|
|
37
61
|
}
|
|
38
62
|
|
|
39
63
|
}
|
package/lib/SAXEvent.js
CHANGED
|
@@ -196,6 +196,24 @@ const SAXEvent = class {
|
|
|
196
196
|
|
|
197
197
|
}
|
|
198
198
|
|
|
199
|
+
get isStartElement () {
|
|
200
|
+
|
|
201
|
+
return this.type === START_ELEMENT
|
|
202
|
+
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
get isEndElement () {
|
|
206
|
+
|
|
207
|
+
return this.type === END_ELEMENT
|
|
208
|
+
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
get isCharacters () {
|
|
212
|
+
|
|
213
|
+
return this.type === CHARACTERS
|
|
214
|
+
|
|
215
|
+
}
|
|
216
|
+
|
|
199
217
|
}
|
|
200
218
|
|
|
201
219
|
SAXEvent.TYPES = {
|
package/lib/XMLNode.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const SAXEvent = require ('./SAXEvent.js')
|
|
2
2
|
const AttributesMap = require ('./AttributesMap')
|
|
3
3
|
const NamespacesMap = require ('./NamespacesMap')
|
|
4
|
+
const MoxyLikeJsonEncoder = require ('./MoxyLikeJsonEncoder')
|
|
4
5
|
|
|
5
6
|
const XML_READER = Symbol ('_xmlReader')
|
|
6
7
|
const ATTRIBUTES = Symbol ('_attributes')
|
|
@@ -31,8 +32,9 @@ const XMLNode = class extends SAXEvent {
|
|
|
31
32
|
|
|
32
33
|
e [PARENT] = this [PARENT]
|
|
33
34
|
e [LEVEL] = this [LEVEL]
|
|
34
|
-
|
|
35
|
-
e [
|
|
35
|
+
|
|
36
|
+
if (ATTRIBUTES in this) e [ATTRIBUTES] = this [ATTRIBUTES]
|
|
37
|
+
if (NS_MAP in this) e [NS_MAP] = this [NS_MAP]
|
|
36
38
|
|
|
37
39
|
return e
|
|
38
40
|
|
|
@@ -96,7 +98,9 @@ const XMLNode = class extends SAXEvent {
|
|
|
96
98
|
|
|
97
99
|
get namespaceURI () {
|
|
98
100
|
|
|
99
|
-
|
|
101
|
+
const {namespacesMap} = this; if (namespacesMap == null) return null
|
|
102
|
+
|
|
103
|
+
return namespacesMap.getNamespaceURI (this.name, true)
|
|
100
104
|
|
|
101
105
|
}
|
|
102
106
|
|
|
@@ -138,4 +142,6 @@ XMLNode.getLocalName = name => {
|
|
|
138
142
|
|
|
139
143
|
}
|
|
140
144
|
|
|
145
|
+
XMLNode.toObject = MoxyLikeJsonEncoder
|
|
146
|
+
|
|
141
147
|
module.exports = XMLNode
|
package/lib/XMLReader.js
CHANGED
|
@@ -14,7 +14,7 @@ const XMLReader = class extends Transform {
|
|
|
14
14
|
options.decodeStrings = false
|
|
15
15
|
options.objectMode = true
|
|
16
16
|
|
|
17
|
-
if (!('stripSpace' in options)) options.stripSpace =
|
|
17
|
+
if (!('stripSpace' in options)) options.stripSpace = ('filterElements' in options)
|
|
18
18
|
assert (options.stripSpace === true || options.stripSpace === false, 'options.stripSpace must be boolean, not ' + typeof options.stripSpace)
|
|
19
19
|
|
|
20
20
|
if (!('useEntities' in options)) options.useEntities = true
|
package/package.json
CHANGED
package/test/soap.xml
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
<SOAP-ENV:Header/>
|
|
3
3
|
<SOAP-ENV:Body>
|
|
4
4
|
<SendRequestRequest xmlns="urn://x-artefacts-smev-gov-ru/services/message-exchange/types/1.1" xmlns:ns0="urn://x-artefacts-smev-gov-ru/services/message-exchange/types/basic/1.1">
|
|
5
|
-
<SenderProvidedRequestData Id="Ue7e71ce1-7ce3-4ca5-a689-1a8f2edbb1af">
|
|
5
|
+
<SenderProvidedRequestData Id="Ue7e71ce1-7ce3-4ca5-a689-1a8f2edbb1af" IDDQD="">
|
|
6
6
|
<MessageID>3931cda8-3245-11ec-b0bc-000c293433a0</MessageID>
|
|
7
7
|
<ns0:MessagePrimaryContent>
|
|
8
8
|
<ExportDebtRequestsRequest xmlns="urn:dom.gosuslugi.ru/debt-responses/1.0.0" xmlns:ns0="urn:dom.gosuslugi.ru/common/1.2.0">
|
package/test/test.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const fs = require ('fs')
|
|
2
2
|
const assert = require ('assert')
|
|
3
|
-
const {XMLReader, SAXEvent, XMLLexer, AttributesMap,
|
|
3
|
+
const {XMLReader, SAXEvent, XMLLexer, AttributesMap, XMLNode} = require ('../')
|
|
4
4
|
|
|
5
5
|
async function test_001_lexer_sync (fn) {
|
|
6
6
|
|
|
@@ -59,8 +59,12 @@ console.log (xml)
|
|
|
59
59
|
|
|
60
60
|
const sax = new XMLReader ({
|
|
61
61
|
// stripSpace: true,
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
filterElements: 'SendRequestRequest',
|
|
63
|
+
map: XMLNode.toObject ({
|
|
64
|
+
// wrap: 1,
|
|
65
|
+
getName: (localName, namespaceURI) => (!namespaceURI ? '' : '{' + namespaceURI + '}') + localName,
|
|
66
|
+
map: o => Object.fromEntries (Object.entries (o).map (([k, v]) => [k + '111', v]))
|
|
67
|
+
})
|
|
64
68
|
})
|
|
65
69
|
|
|
66
70
|
/*
|
|
@@ -77,26 +81,22 @@ console.log (xml)
|
|
|
77
81
|
})
|
|
78
82
|
*/
|
|
79
83
|
|
|
80
|
-
|
|
81
|
-
// sax.process (fs.createReadStream ('test/' + fn))
|
|
82
|
-
|
|
83
|
-
sax.process (xml)
|
|
84
|
-
|
|
84
|
+
|
|
85
85
|
//console.log (sax)
|
|
86
86
|
//console.log (sax.isSAX)
|
|
87
87
|
|
|
88
|
-
|
|
88
|
+
/*
|
|
89
89
|
let s = ''
|
|
90
|
-
for await (const
|
|
90
|
+
for await (const e of sax) {
|
|
91
91
|
s += xml
|
|
92
|
-
// console.log ([type,
|
|
92
|
+
// console.log ([e.type, e.isStartElement, e.isEndElement , e.isCharacters])
|
|
93
93
|
}
|
|
94
|
+
*/
|
|
95
|
+
//console.log ([xml, s])
|
|
94
96
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
// const v = await sax.findFirst ()
|
|
97
|
+
const v = await sax.process (xml).findFirst ()
|
|
98
98
|
|
|
99
|
-
|
|
99
|
+
console.log (JSON.stringify (v, null, 2))
|
|
100
100
|
|
|
101
101
|
}
|
|
102
102
|
|