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

squash-xml-json

v0.1.0

Published

A library to convert XML data to a flat JSON structure, and back.

Downloads

5

Readme

Squash XML JSON

A library to convert XML data to a flat JSON structure, and back.

This is a library suited for those who think of XML as an asset, not a nuisance. It aims to offers a simple and efficient way of manipulating XML documents without a DOM.

Squash XML JSON can handle:

  • mixed content
  • namespaces and namespace prefixes
  • two-way transformation XML <=> JSON

Dependencies

  • requires sax.js (browser build will bundle it)

Installation

npm install squash-xml-json

To build and test:

npm run build && npm test

# to build for use in browser
npm run build-web

Usage

ES6

import {flattenXML, hydrateXML} from "squash-xml-json"

node

var sxj = require("squash-xml-json")

Browser

<script src="squash-xml-json-web.js"></script>
<script>console.log(sxj)</script>

XML to JSON: flattenXML()

Squash XML JSON creates a flat structure of "nodes", each identified by an UUID or an element's xml:id attribute when present. This simplifies element lookup in the JSON structure.

let xml = `<text xmlns="http://www.tei-c.org/ns/1.0">
      <body>
         <p xml:id="p1">Some <emph>text</emph> here.</p>
      </body>
  </text>`
let json = flattenXML(xml)

This will result in a JSON object containing each XML node. An element will look like this:

{
	'p1': {
		'name': 'p',
		'children': ['>td8340e43',
			'>eeb0d435c',
			'>t11da3f7e'
		],
		'@': {
			'xml:id': 'p1'
		},
		'ns': {
			'': 'http://www.tei-c.org/ns/1.0'
		},
		'parent': '>ed8b1db33',
		'id': 'p1'
	}
}

The element includes its children's and parent's IDs, which can be used to retrieve those nodes directly form the JSON object. Note that the ID of this element p1 is derived from its xml:id attribute, while other elements get assigned a UUID (truncated in the example).

Before root

The IDs of processing instructions and text nodes before the root element are listed in a dedicated property called >before_root.

{
	'>before_root': ['>t8b1ef55d'],
	'>t8b1ef55d': {
		'pi' {
			name: 'xml',
			'body': 'version="1.0" encoding="utf-8"'
		}
	}
}

Options

Set skipSpace to true to exclude empty text nodes. Do this if you want a leaner JSON and don't care about pretty printing or preserving original spacing.

flattenXML(xml, {skipSpace : true})

JSON to XML: hydrateXML()

Use hydrateXML To serialize a compliant JSON structure back into an XML string:

let json = {
	'>before_root': [],
	'>ee0c1ac32': {
		name: 'root',
		children: ['>e3cadaf0c'],
		'@': {},
		ns: {},
		parent: null,
		id: '>ee0c1ac32'
	},
	'>e3cadaf0c': {
		name: 'el',
		children: ['>t62d02a9f'],
		'@': {},
		ns: {},
		parent: '>ee0c1ac32',
		id: '>e3cadaf0c'
	},
	'>t62d02a9f': {
		t: 'text',
		parent: '>e3cadaf0c'
	}
}

hydrateXML(json)

//returns "<root><el>text</el></root>"