Sunday 25 March 2012

Shell script code for xml to json and json to xml converter ,and parser


flg=0
xmlv ()
{
xmllint --shell /home/proton/ass7/assignment7/$1 <<<"du" >a
for i in 1 2
do
mv a b
sed 1d b >a
rm b

done
mv a b
sed '$d' b >a
rm b
cat a
echo "enter the path of the record whose entity interests you "
read pat
xmllint --shell /home/proton/ass7/assignment7/$1 <<< "xpath "$pat"" > c
grep -q "0 nodes" c
if [ $? -eq 0 ] ; then
echo "no such path exist"
exit 1
fi
grep -q "ATTRIBUTE " c
if [ $? -eq 0  ] ; then

flg=1
for i in 1 2 3 4
do
mv c b
sed 1d b >c
rm b
done
mv c b
sed '$d' b >c
rm b
cat c
fi
grep -q "1  ELEMENT" c
if [ $? -eq 0 ] ; then
#echo "1 elem"
flg=1
xmllint --shell --noblanks /home/proton/ass7/assignment7/$1 <<< "xpath  "$pat/'text()'"" > c
for i in 1 2 3
do
mv c b
sed 1d b >c
rm b
done
mv c b
sed '$d' b >c
rm b
cat c

r1=`grep -c "content= " c`
r2=`grep -c "content" c`
r3=`grep -c "contains 0 nodes" c`

if [ $? -gt 0 ]; then
if [ $flg -eq 0 ]
then
echo "no datum"
fi
fi
[ -s c ]
if [ $? -eq 0 ]; then
if [ $flg -eq 0 ]
then
echo "no datum"
fi
fi


fi
}

clear

echo "Xml | json reader @ 1"
echo "xml -> json @ 2"
echo "json -> xml @ 3"
read cho
if [ $cho -eq 1 ]
then
echo "file list"
ls /home/proton/ass7/assignment7/files

echo "enter file name"
read fil


if [ "${fil#*.}" = "xml" ]
then
echo "File type is xml"
echo " "
echo "listing the records"
cp "/home/proton/ass7/assignment7/$fil" d
xmlv d

elif [ "${fil#*.}" = "json" ]
then
echo "File type is json"
JSON=`cat "/home/proton/ass7/assignment7/$fil"`

echo "$JSON" | ./try.py -r vin >d
mv d b
sed 1d b >d
rm b
mv d b
sed '$d' b >d
rm b
xmlv d
else
echo "error in file type "
exit 1
fi

fi

if [ $cho -eq 3 ]
then
echo "enter file name"
read fil
JSON=`cat "/home/proton/ass7/assignment7/$fil"`

echo "$JSON" | ./try.py -r vin >d
mv d b
sed 1d b >d
rm b
mv d b
sed '$d' b >d
rm b
cat d


fi
if [ $cho -eq 2 ]
then
echo "enter file name"
read fil
xmllint --noblanks /home/proton/ass7/assignment7/d >a
mv a b
sed 1d b >a
rm b
tr -d '\n' < a >b
tr -d ' ' < b >a
rm b
xmlx=`cat "/home/proton/ass7/assignment7/a"`

rhino '/home/proton/ass7/assignment7/xml2json.js' "$xmlx"

fi

 if [ $cho -gt 3 ]
then
echo "error"
fi

 if [ $cho -lt 0 ]
then
echo "error"
fi





Please note /home/proton/ass7/assignment7 is a folder path ,you may use your path here

 " mv a b
sed 1d b >a
rm b "
is used to remove certain lines from the temporary file before parsing sed $d refer to the last line,There are efficient methods to use the same without looping but this being easy to illustrate and understand is hence used.

"<<<" in the xmllint instruction refers to the shell instruction "shell within the terminal"

 `grep -c "content= " c` are used to identify the pattern and please note the quotes are found below the tilde sign in the keyboard indicating its an instruction and not a string

$? -gt 0 usage in " if " after grep will cause by default to store the return value of grep instruction in $?



No comments:

Post a Comment