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

basic-xml2json

v1.0.4

Published

Basic parser to convert XML strings to JSON

Downloads

110

Readme

basic-xml2json

Simple parser to convert XML strings to JSON.

Usage

var xml2json = require('basic-xml2json');

parse

This code is based off the xml-parser here: https://github.com/segmentio/xml-parser. Please see that page for details of the JSON output.

The parse function takes an XML string (mandatory) and an object describing the options (optional).

var json = xml2json.parse(xml, options);

Or

var json = xml2json.parse(xml);
parse:options
  • removeNamespacePrefixes

    Boolean, defaults to true. Set this to false if you want to retain xml namespace prefixes in the JSON output.

    var options = { removeNamespacePrefixes: false };
    	

getChildNodes

Get an array of matching nodes under the specified parent node for the specified path.

var json = xml2json.parse(xml);
var nodes = xml2json.getChildNodes(json.root, ['Body','Response','Address']);
	

getChildNode

Get the first matching node under the specified parent node for the specified path.

var json = xml2json.parse(xml);
var address = xml2json.getChildNode(json.root, ['Body','Response','Address']);
	

getContent

Get the value of the first matching node under the specified parent node for the specified path. Encoded XML values will are decoded.

var json = xml2json.parse(xml);
var address = xml2json.getChildNode(json.root, ['Body','Response','Address']);
var suburb = xml2json.getContent(address, 'Suburb');

Or var suburb = xml2json.getContent(json.root, ['Body','Response','Address','Suburb']);

getRawContent

Just like getContent except that encoded XML values are not decoded.

var suburb = xml2json.getRawContent(json.root, ['Body','Response','Address','Suburb']);
	

getAllContent

Get an array of values for the matching nodes under the specified parent node for the specified path. Encoded XML values will are decoded.

var json = xml2json.parse(xml);
var suburbs = xml2json.getAllContent(json.root, ['Body','Response','Address','Suburb']);

decodeXML

Decode some text.

var decodedText = decodeXML(text);

encodeXML

Encode some text.

var encodedText = encodeXML(text);

Wildcard Matching

Probably rarely useful, but if you have a situation where you need to match elements with different paths (but at the same depth) then you can use a wildcard match. For example:

<xmldoc>
	<postalAddress>
		<suburb>Brisbane</suburb>
	</postalAddress>
	<billingAddress>
		<suburb>Sydney</suburb>
	</billingAddress>
</xmldoc>

var json = xml2json.parse(xml);
var suburbs = xml2json.getAllContent(json.root, [xml2json.ANY, 'suburb']);

In this example, suburbs will be ['Brisbane','Sydney'].