Dan Newcome, blog

I'm bringing cyber back

Jath – A JSON template language for XML processing

with 4 comments

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.

Advertisements

Written by newcome

April 19, 2010 at 11:44 pm

Posted in Uncategorized

4 Responses

Subscribe to comments with RSS.

  1. […] 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 […]

  2. […] 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 […]

  3. 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

  4. @Siju – I posted some code on your Github issue that answers your question.

    I’m reposting it here for reference:

      var template = [
            '//label', { 'id': '@id', 'added': '@added',
                'address': [ 'address', { 'street': 'street', 'city': 'city' } ]
        } ];
    

    newcome

    June 22, 2012 at 1:11 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: