Skip to content

Commit d2419b9

Browse files
author
Dmitry Ovsyanko
committed
README.md updated
1 parent 7c032f6 commit d2419b9

1 file changed

Lines changed: 34 additions & 45 deletions

File tree

README.md

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@ It features both (fast and simple, but greedy) [synchronous](https://github.com/
1414
npm install xml-toolkit
1515
```
1616

17-
# Using
18-
19-
* [Parsing a small file completely](XMLParser)
17+
# Usage
18+
* [Parsing a small file completely](XMLParser) (optionally validating)
2019

2120
```js
2221
const fs = require ('fs')
23-
const {XMLParser} = require ('xml-toolkit')
22+
const {XMLParser, XMLSchemata} = require ('xml-toolkit')
2423

2524
const xml = fs.readFileSync ('doc.xml')
26-
const parser = new XMLParser ({...options})
25+
// const xs = new XMLSchemata ('schema.xsd')
26+
27+
const parser = new XMLParser ({
28+
// xs,
29+
})
2730

2831
const document = parser.process (xml)
2932

@@ -32,12 +35,15 @@ for (const element of document.detach ().children) {
3235
}
3336
```
3437

35-
* [Reading a Record List](https://github.com/do-/node-xml-toolkit/wiki/Use-Case:-Reading-a-Record-List), streaming mode
38+
* [Reading a Record List](https://github.com/do-/node-xml-toolkit/wiki/Use-Case:-Reading-a-Record-List) (optionally validating)
3639

3740
```js
38-
const {XMLReader, XMLNode} = require ('xml-toolkit')
41+
const {XMLReader, XMLNode, XMLSchemata} = require ('xml-toolkit')
42+
43+
// const xs = new XMLSchemata ('schema.xsd')
3944

4045
const records = new XMLReader ({
46+
//xs,
4147
filterElements : 'Record',
4248
map : XMLNode.toObject ({})
4349
}).process (xmlSource)
@@ -57,7 +63,7 @@ const records = new XMLReader ({
5763
// records.on ('data', record => doSomethingWith (record))
5864
```
5965

60-
* [Getting a Single Element](https://github.com/do-/node-xml-toolkit/wiki/Use-Case:-Getting-a-Single-Element), streaming mode
66+
* [Getting a Single Element](https://github.com/do-/node-xml-toolkit/wiki/Use-Case:-Getting-a-Single-Element)
6167

6268
```js
6369
const {XMLReader, XMLNode} = require ('xml-toolkit')
@@ -68,6 +74,21 @@ const data = await new XMLReader ({
6874
}).process (xmlSource).findFirst ()
6975
```
7076

77+
* [Formatting XML](https://github.com/do-/node-xml-toolkit/wiki/XMLPrinter)
78+
```js
79+
const {XMLParser} = require ('xml-toolkit')
80+
xml = new XMLParser ()
81+
.process (fs.readFileSync ('doc.xml'))
82+
.toString ({
83+
// decl: {encoding: 'UTF-8', standalone: 1},
84+
space: '\t',
85+
// attrSpace: 2,
86+
// EOL: '\n',
87+
// level: 0,
88+
// encodeLineBreaks: false,
89+
})
90+
```
91+
7192
* [Patching XML](https://github.com/do-/node-xml-toolkit/wiki/Use-Case:-Patching-XML)
7293

7394
```js
@@ -81,8 +102,6 @@ let xmlResult = ''; for await (const node of new XMLReader ().process (xmlSource
81102
* [Serializing an Object According to an XML Schema](https://github.com/do-/node-xml-toolkit/wiki/Use-Case:-Serializing-an-Object-According-to-an-XML-Schema)
82103

83104
```js
84-
const {XMLSchemata} = require ('xml-toolkit')
85-
86105
const data = {ExportDebtRequestsResponse: {
87106
"request-data": {
88107
// ...
@@ -99,47 +118,20 @@ const xml = xs.stringify (data)
99118
<!-- ... and so on ... -->
100119
*/
101120
```
102-
103-
* Invoking a [SOAP 1.1](https://github.com/do-/node-xml-toolkit/wiki/SOAP11) or [SOAP 1.2](https://github.com/do-/node-xml-toolkit/wiki/SOAP12) Web Service
121+
* Invoking a [SOAP 1.1](SOAP11) Web Service
104122
105123
```js
106124
const http = require ('http')
107-
const {SOAP11, SOAP12} = require ('xml-toolkit')
125+
const {SOAP11} = require ('xml-toolkit')
108126

109-
const soap = new SOAP11 ('their.wsdl') // or SOAP12
127+
const soap = new SOAP11 ('their.wsdl')
110128

111129
const {method, headers, body} = soap.http ({RequestElementNameOfTheirs: {amount: '0.01'}})
112130

113131
const rq = http.request (endpointURL, {method, headers})
114132
rq.write (body)
115133
```
116134
117-
* [Implementing](https://github.com/do-/node-xml-toolkit/wiki/Use-Case:-Implement-a-SOAP-Web-Service) a SOAP service
118-
119-
```js
120-
const {XMLSchemata, SOAP11, SOAP12, SOAPFault} = require ('xml-toolkit')
121-
122-
const SOAP = SOAP11 // or SOAP12
123-
124-
const xs = new XMLSchemata (`myService.wsdl`)
125-
126-
let body, statusCode; try {
127-
body = xs.stringify (myMethod (/*...*/))
128-
statusCode = 200
129-
}
130-
catch (x) {
131-
body = new SOAPFault (x)
132-
statusCode = 500
133-
}
134-
135-
rp.writeHead (statusCode, {
136-
'Content-Type': SOAP.contentType,
137-
})
138-
139-
const xml = SOAP.message (body)
140-
rp.end (xml)
141-
```
142-
143135
# Motivation
144136
145137
Unlike Java (with [JAXB](https://www.oracle.com/technical-resources/articles/javase/jaxb.html) and [JAX-WS](https://www.oracle.com/technical-resources/articles/javase/jax-ws-2.html)) and some other software development platforms dating back to late 1990s, the core node.js library doesn't offer any standard tool for dealing with XML.
@@ -155,9 +147,6 @@ Pure js 3rd party modules are abundant, but after some real tasks based research
155147
156148
No W3C specification is 100% implemented here. For instance, DTDs are not supported, so, in theory, any rogue XML file using such bizarre deprecated feature as [Entity Declarations](https://www.w3.org/TR/xml/#sec-entity-decl) may crash the local XML parser.
157149
158-
Though `node-xml-toolkit` has some support for [XMLSchema](https://github.com/do-/node-xml-toolkit/wiki/XMLSchema), it cannot be used for validation. Here, XML Schema is used only as a template for outputting valid XML provided a correct set of input data. That means, each `decimal` will be formatted with proper `fractionDigits`, but no CPU cycle will be spent on checking whether the incoming 10 char string fully conforms to the [`date`](https://www.w3.org/TR/2012/REC-xmlschema11-2-20120405/datatypes.html#date) lexical representation or not.
159-
160-
In short, `node-xml-toolkit` may produce incorrect results for some input data, especially for deliberately broken ones.
150+
The [XMLSchema](https://github.com/do-/node-xml-toolkit/wiki/XMLSchemata) is fairly usable for most real world cases, still some features (like `xs:unique`) are ignored completely, some others may require more test coverage. In general, XML Schema support in `xml-toolkit` should be considered _forever beta_.
161151
162-
There are perfectly reliable external tools for XML validation: for instance,
163-
[xmllint](https://linux.die.net/man/1/xmllint) (used in the test suite here) do just fine.
152+
There are perfectly reliable external tools for XML validation: for instance, [xmllint](https://linux.die.net/man/1/xmllint) (used in the test suite here) do just fine.

0 commit comments

Comments
 (0)