npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

xml-toolkit

v1.0.45

Published

XML parser, marshaller, SOAP adapter

Downloads

3,499

Readme

workflow Jest coverage

node-xml-toolkit is a pure node.js library for solving diverse XML related application tasks, e. g.:

  • scanning through multi gigabyte long XML files with a limited memory buffer;
  • invoke SOAP Web services given a WSDL, method name and a plain data object.

It features both (fast and simple, but greedy) synchronous and (trickier to use, but robust and streaming capable) asynchronous XML parsers along with various tools for writing well formed XML: according to a schema or without any.

Installation

npm install 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 {XMLSchemata} = require ('xml-toolkit')

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, SOAP12} = require ('xml-toolkit')

const soap = new SOAP11 ('their.wsdl') // or SOAP12

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

const rq = http.request (endpointURL, {method, headers})
rq.write (body)
const {XMLSchemata, SOAP11, SOAP12, SOAPFault} = require ('xml-toolkit')

const SOAP = SOAP11 // or SOAP12

const xs = new XMLSchemata (`myService.wsdl`)

let body, statusCode; try {  
  body = xs.stringify (myMethod (/*...*/))
  statusCode = 200
}
catch (x) {
  body = new SOAPFault (x)
  statusCode = 500
}

rp.writeHead (statusCode, {
  'Content-Type': SOAP.contentType,
})

const xml = SOAP.message (body)
rp.end (xml)

Motivation

Unlike Java (with JAXB and JAX-WS) 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.

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 after some real tasks based researches 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;
  • minimum external dependencies.

Limitations

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 may crash the local XML parser.

Though node-xml-toolkit has some support for 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 lexical representation or not.

In short, node-xml-toolkit may produce incorrect results for some input data, especially for deliberately broken ones.

There are perfectly reliable external tools for XML validation: for instance, xmllint (used in the test suite here) do just fine.