Introduction

This program is used as a command line tool to examine and update XML files in a shell script. The best way to describe it is to do some examples...

Imagine an XML file as follows:

<web_app>
        <description>Libra Web Application</description>
        <context_param>
                <param_name>swapFlag</param_name>
                <param_value>all_https</param_value>
        </context_param>
        <context_param>
                <param_name>httpPort</param_name>
                <param_value>80</param_value>
        </context_param>
        <context_param>
                <param_name>httpsPort</param_name>
                <param_value>443</param_value>
        </context_param>
        <rest_of_file>
                <data_example>This is the rest</data_example>
                <data_example>Of this example</data_example>
        </rest_of_file>
</web_app>

First - a simple modification command. - xmlproc. Please note: The following "xmlproc" is one command - note the "\" at the end of lines.

xmlproc  web.xml \
         xmatch /                                          \
             va element context_param                      \
                element param_name text "swapFlag"         \
         xmatch va                                         \
             vb element param_value                        \        
         xsettext vb all_http                              \
         xsavefile   webnew.xml

This changes the "all_https" string in the above web.xml file to "all_http" and saves it to a file called "webnew.xml".

Secondly, the commands "xmlopen" "xmlcmd" and "xmlclose" exist for more sophistication in shellscripts: In this, please note the "\" at the end of some lines here too.

#!/bin/bash

XMLHANDLE=`xmlopen web.xml`

xmlcmd $XMLHANDLE \
       xmatch /                                  \
          va element context_param               \
             element param_name text "httpPort"

if ! test xmlcmd $XMLHANDLE xifexists va
then
        echo "Context parameter for httpPort does not exist"
        exit 1
fi

xmlcmd $XMLHANDLE \
       xmatch va                                  \
          vb element   param_value

VB=`xmlcmd $XMLHANDLE xgettext  vb`

if test "$VB" != 7777
then
        xmlcmd $XMLHANDLE xsettext vb 7777
fi

xmlcmd $XMLHANDLE xsavefile webnew.xml

xmlclose $XMLHANDLE

This changes the "80" string in the above web.xml file to "7777" and saves it to a file called "webnew.xml". It also does some (rudimentary) error checking.

SourceForge.net Logo