Skip to content
do- edited this page Dec 10, 2022 · 55 revisions

node-xml-toolkit is a node.js library for solving routine software integration tasks like:

  • invoking and implementing SOAP Web services;
  • quickly reading necessary fragments from multi-GB XML files.

Unfortunately, unlike Java (with JAXB and JAX-WS) and some other software development platforms, till now, the core node.js library doesn't offer any standard tool for dealing with XML. It might be a binding of a well known external library (libxml comes to mind first -- as it's built in PostgreSQL in many popular distros, for example), but, alas, nothing viable of this sort seem to be available.

Pure js 3rd party modules are abundant, but something that worth using instead of reimplementing is really hard to find. At very best, some of them solve isolated problems exposing non-standard APIs and adding some compound dependencies to your project.

This is why the author decided to start up yet another node.js DIY XML toolkit project to get the job done with:

  • minimum computing resources,
  • minimum lines of application code and
  • minimum external dependencies.

Installation

npm i xml-toolkit

Using

const fs = require ('fs')
const {XMLParser} = require ('xml-toolkit')

const xml    = fs.readFileSync ('doc.xml')
const parser = new XMLParser  ({...options})

const document = parser.process (xml)

for (const element of document.detach ().children) {
  console.log (element.attributes)
}
const {XMLReader, XMLNode} = require ('xml-toolkit')

const records = new XMLReader ({
  filterElements : 'Record', 
  map            : XMLNode.toObject ({})
}).process (xmlSource)

// ...then:
// await someLoader.load (records)

// ...or
// for await (const record of records) { // pull parser mode

// ...or
// records.on ('error', e => console.log (e))
// records.pipe (nextStream)

// ...or
// records.on ('error', e => console.log (e))
// records.on ('data', record => doSomethingWith (record))
const {XMLReader, XMLNode} = require ('xml-toolkit')

const data = await new XMLReader ({
  filterElements : 'MyElementName', 
  map            : XMLNode.toObject ({})
}).process (xmlSource).findFirst ()
const {XMLReader} = require ('xml-toolkit')

let xmlResult = ''; for await (const node of new XMLReader ().process (xmlSource)) xmlResult += 
    node.isCharacters && node.parent.localName === 'ThePlaceHolder' ? id : 
    node.xml
const data = {ExportDebtRequestsResponse: {	
  "request-data": {
   // ...
  }
}

const xs = new XMLSchemata ('xs.xsd')

const xml = xs.stringify (data)

/* result:
<ns0:ExportDebtRequestsResponse xmlns:ns0="urn:...">
  <ns0:request-data>
    <!-- ... and so on ... -->
*/
const http = require ('http')
const {SOAP11} = require ('xml-toolkit')

const soap = new SOAP11 ('their.wsdl')

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

const rq = http.request (endpointURL, {method, headers})
rq.write (body)

Components

References