Jath – A JSON template language for XML processing
On Saturday, I released a project called Jath that implements a technique for processing XML data that I have been playing with. The idea is to be able to write a concise declarative template in JSON that describes the desired Javascript object and apply the template to the source XML data.
Using a template like this:
[ "//label", { id: "@id", added: "@added", address: { street: "address/street", city: "address/city" } } ]
we can transform XML
<labels> <label id='ep' added="2003-06-10"> <name>Ezra Pound</name> <address> <street>45 Usura Place</street> <city>Hailey</city> <province>ID</province> </address> </label> <label id='tse' added="2003-06-20"> ... </label> ... </labels>
Into Javascript like this
[ { id: "ep", added: "2003-06-10", address: { street: "45 Usura Place", city: "Hailey" } }, { id:"tse", added: "2003-06-20", address: { street: "3 Prufrock Lane", city: "Stamford" } }, ... ]
The current implementation now supports XML namespace prefixes in all browsers except Internet Explorer. Jath also offers full support for Mozilla, Chrome, Safari, and nearly full support for Opera.
More usage information is available on the Github project page Jath. More examples can be found by looking at the test cases in samples.html. All code is provided under the MIT open source license.
[…] looked around for something that would let me do something similar to what I did in Jath to go from XML to JSON. I found an interesting hack for making jQuery work against JS objects, but […]
Transforming JSON to XML using Mustache « Dan Newcome, blog
August 18, 2010 at 7:51 pm
[…] of my blog may recall a project that I created almost a year ago called Jath, which is an XML-to-JSON conversion library that works in the browser (and now in Node.js also). My […]
Consuming XML Web services in Backbone.js using Jath « Dan Newcome, blog
February 20, 2011 at 5:13 am
how can we write a template for the following xml
My XML
Ezra Pound
45 Usura Place
Hailey
ID
Siju
3 Prufrock Lane
Stamford
ID
2nd address
2nd city
2nd id
3rd address
3rd city
3rd id
…
Into Javascript like this
view source
print?
[
{
id: “ep”,
added: “2003-06-10”,
address: {
street: “45 Usura Place”,
city: “Hailey”
}
},
{
id:”ep2″,
added: “2003-06-20”,
address:[
{
street: “3 Prufrock Lane”,
city: “Stamford”
},
{
street: “2nd address”,
city: “2nd city”
},
{
street: “3rd address”,
city: “3rd city”
}
},
…
]
Siju
June 22, 2012 at 4:14 am
@Siju – I posted some code on your Github issue that answers your question.
I’m reposting it here for reference:
newcome
June 22, 2012 at 1:11 pm