Code Monkey home page Code Monkey logo

xml-to-json's Introduction

Xml-to-JSON

Very simple utility to convert XML string or XMLDocument into a JSON object

Setup

Just include xml.js or xml.min.js on your page.

About the output

As we all know XML and JSON are different. Not only in verbosity, but also in the way data is store and represented. This utility does it's best to output JSON structure as close as possible to the source, but there are things you should know about, so here we go.

  • Handling node attributes - In XML attributes are stored as part of the node tag and thus allow for their names to be the same as node names (i.e <name name="sam"/>), this is considered a valid XML, in JSON however this would mean a big no-no, because matching names would simply overwrite each other. To avoid this confilct, all attribute names are prefixed with "@" sign. - Ex. <name name="sam"/> => {"name":{"@name":"sam"}};

  • Handling node value and CDATA - Node value is the Text between the open/close of a tag, however there are cases where node may contain CDATA element in addition to just text value (i.e. <node>Hello</node>). in this example we don't want to loose any content, so converter will concatinate values of both Text and CDATA nodes. These values will be accessible through a "Text" property. - Ex. <node>Hello</node> => {"node":{"Text":"Hello World"}}.

  • Root node handling - Converter will create a root node element at the top of the JSON structure. - Ex. <root><node>Hello</node></root> => {"root":{"node":{"Text":"Hello World"}}}.

Examples

Using with string XML

var strXML = "<root>" +
		"<group>" +
			"<item name=\"test1\">test data 1</item>"+
			"<item name=\"test2\">test data 2</item>"+
			"<item name=\"test3\">test data 3</item>"+
		"</group>"+
	"</root>";
	jsonData = xml.xmlToJSON(strXML);
	console.dir(jsonData);
	console.log(JSON.stringify(jsonData));

Using with AJAX data

$.ajax({
	url: "data/file.xml",
	dataType:"xml"
}).done(function(xmlData){
	var jsonData = xml.xmlToJSON(xmlData);
	console.dir(jsonData);
	console.log(JSON.stringify(jsonData));
});

Using as AJAX dataFilter

$.ajax({
	url: "data/file.xml",
	dataType:"xml",
	dataFilter: xml.xmlToJSON
}).done(function(jsonData){
	console.dir(jsonData);
	console.log(JSON.stringify(jsonData));
});

Using as jQuery Plugin

$.ajax({
	url: "data/file.xml",
	dataType:"xml"
}).done(function(xmlData){
	var jsonData = $.xmlToJSON(xmlData);
	console.dir(jsonData);
	console.log(JSON.stringify(jsonData));
});

xml-to-json's People

Contributors

stsvilik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

xml-to-json's Issues

Collisions of node names with attrs

I am not quite sure what you mean by the node names colliding with attribute names and thus requiring attributes to be escaped with '@'. Wouldn't this: <node node="attr"></node> become this: { node: { node: "attr" } }? There is no collision - it is nested; or am I missing something?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.