Sunday 25 March 2012

A shell script for Xml to json , Json to Xml converter and parser for accessing record data

This script can also be used to convert RSS to JSON , or any XML to JSON

The shell script does the following things:

1. Display the records to the user.
2. User can query for value of any particular record.
3. Convert from one format to other.

The Designing strategy followed was
The designing algorithm for xml to json conversion and vice versa involves
the mapping from one format to other after identifying the tag nodes and
their elements the identification of which follows certain regular expression
statements ,which in this case is implemented by utlisation of a foreign sript
in the shell , a python script and a javascript are utilised ,they being procured
from a public open source domain and tailored to the needs of this problem.
Both of this scripts invariably use the regular expression for parsing the xml
and json for conversion as the tags their attributes and the elements are
converted from the xml to their respective place holders in the json.However
the attribute in the xml is treated as one more of its element ,though in the
data transfer by utilisation of the xml this does not alter the originality or
the information of the xml content.Thus the conversion process treats the
attributes just as same as the elements.
A linux tool is used for xml parsing this being the xmllint which allows the
utilisation of xpath wherin the user has to provide the path data for accessing
the record data. Another tool of importance is rhino which allows the js file
to run in the linux environment.
The shell script as such utilises certain temporary files to store the xml
and json data and the partially parsed values also grep functions are used to
eliminate certain redundant data to appreciate the pretty printing for xml
and json files. A separate json parser is not implemented as the motivation
was to use the xmllint itself thus the conversion of the json to xml before its
parsing is carried out

Apart from xmllint another intersting tool was the rhino from mozilla guys ,it allows you to run a javascript in the linux terminal
You just got to install rhino by using "sudo apt-get install rhino" then specify to run the javascript

rhino "path to .js file"  "any argument if any "

To use this shell script you need to install python and xmllint ,use the sudo apt-get to do so.

Xml file parser



Json file parser


Xml to json converter

note here you can actually save the display in a file by using the >filename command in the shellscript where required




Json to xml converter


The shell script code

The python script code

The javascript,code


2 comments:

  1. Really helped me out, i totally respect you for sharing this

    ReplyDelete
  2. Awesome ,i can use this in my php programming now!!!

    ReplyDelete