Socket
Socket
Sign inDemoInstall

basic-xml2json

Package Overview
Dependencies
2
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    basic-xml2json

Basic parser to convert XML strings to JSON


Version published
Weekly downloads
98
decreased by-43.35%
Maintainers
1
Install size
899 kB
Created
Weekly downloads
 

Readme

Source

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'].

Keywords

FAQs

Last updated on 24 Dec 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc