Output as XML File

Hello All,

I am running a shell script, which in turn runs a perl script. When this perl script runs successfully, the output is generated as an XML file. The following is a snippet of my code:

 
....
....
...
if [ condition is true] ;
then
    perl myScript.pl
    echo "<statistics>"
    echo " <update>YES</update>"
    echo " <date>`date +\%m\%d\%Y.\%H\%M\%S`</date>"
    echo " <id>$id</id>"
    echo " <name>$name</name>"
    echo "</statistics>" 
....
...
..

To run this shell script (and redirect the output in an xml file)

 
./test.sh ARG1 >> output.xml

The output XML file does not get displayed properly, since it does not have a 'parent tag'. If I manually add this tag, I can display the file:

 
 
XML File (does not displays without a parent tag <master_log>):
<statistics>
 <update>NO</update>
 <date>05202012.180001</date>
 <id>my_Local_1</id>
 <name>AAA</name>
</statistics>
<statistics>
 <update>NO</update>
 <date>05212012.134043</date>
 <id>LOCAL_MY_2</id>
 <name>XXX</name>
</statistics>
...
..
.

How can I automatically add tags <master_log></master_log>, without having to do so manually? And also without affecting the log append procedure everytime the script runs successfully?

That is going to be most difficult because you'll need to be inserting inside those tags every time you do it, wouldn't you? That would quickly become a huge amount of work every time you want to just append, since you need to hunt for the last line and delete it before you do so...

Myself, I'd just keep the log as a list of <statistics> elements, and only wrap it when I need to by doing

( echo "<master_log>" ; cat logfile ; echo "</master_log>" ) > newfile

A rare useful use of cat on a single file :wink:

1 Like

Yes, you are right. It is a difficult task. Your way sounds good, and it works.

Thanks!