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

jsontoxml

v1.0.1

Published

This is a library designed to render js objects as xml. Its not made to parse or otherwise edit existing xml/html structures.

Downloads

347,507

Readme

Build Status

browser support

jsontoxml

This is a library designed to render js objects as xml. Its not made to parse or otherwise edit existing xml/html structures. For that and perhaps as a compliment to this you can use jsdom or xml2js for editing existing markup.

this will do a good job rendering json as xml but apis that require xml expect odd things mostly related to elements with attributes and implicit array like keys that make formatting your json a little tricky.

example

var jsonxml = require('jsontoxml');

var xml = jsonxml({
	node:'text content',
	parent:[
		{name:'taco',text:'beef taco',children:{salsa:'hot!'}},
		{name:'taco',text:'fish taco',attrs:{mood:'sad'},children:[
			{name:'salsa',text:'mild'},
			'hi',
			{name:'salsa',text:'weak',attrs:{type:2}}
		]},
		{name:'taco',attrs:'mood="party!"'}
	],
	parent2:{
		hi:'is a nice thing to say',
		node:'i am another not special child node',
		date:function(){
			return (new Date())+'';
		}
	}
})

console.log(xml);

outputs:

<node>text content</node>
<parent>
	<taco>
		beef taco
		<salsa>hot!</salsa>
	</taco>
	<taco mood='sad'>
		fish taco
		<salsa>mild</salsa>
		hi
		<salsa type="2">weak</salsa>
	</taco>
	<taco mood='party!'/>
</parent>
<parent2>
	<hi>is a nice thing to say</hi>
	<node>i am another not special child node</node>
	<date>Sun Sep 26 2010 17:27:29 GMT-0700 (PDT)</date>
</parent2>

API

jsontoxml (obj,options)

  • a valid json structure to interpret or a json string
  • returns an xml string
    • options is optional. it can be true (add generic xml header) or an object. If an object, valid options are:
      • escape
        • calls escape on all values
        • attribute values if attribute values are specified as an object
      • xmlHeader can either be boolan (add generic <?xml …?> header) or an object. If an object valid options are:
        • standalone if true, the <?xml …?> gets an additional attribute standalone="yes".
      • docType if defined gets added as the <!DOCTYPE …> contents (unescaped).
      • prettyPrint if truthy the output gets a rudimentary pretty print (good for debugging, don't expect too much)
      • indent specify what unit you would like to indent by (spaces, tabstop, nothing - pass an empty string)
      • removeIllegalNameCharacters replace illegal XML element Name characters with '_'
      • html instead of adding self closing tags for empty tags add an open and close tag. <salsa/> becomes <salsa></salsa>

jsontoxml.escape (string)

  • returns string with xml entities escaped
  • escapes "" & < >

jsontoxml.cdata (string)

  • wraps string with <![CDATA[ ]]>
  • removes all occurences of close cdata (]]>) in input text

more description

I made this because i wanted to abstract away the fact that antiquated external systems require post data as xml and i wanted to expose a standard js calling api like my other interfaces.

I did not want to instantiate an entire dom to perform simple updates to tags in lower level functions (like injecting api keys) when top level api call specific functions start building the xml string.