How to update a xml with shell script ?

Hey guys, do you remember the script to create xml with shell script ?

Today I will post an example of shell script to update tags in xml file. I used the script in the bottom link as base.


This is the xml that I used as example:


<?xml version="1.0" encoding="ISO-8859-1"?>
<goodnews>
 
 <to>Rafaela</to>
 <from>Br Bank</from>
 <date>04/01/2007</date>
 <amount>$1000,000.00</amount>
 <account>0024549Y48K3-843</account>
 <message>We are pleased to inform you that the above amount was transferred 
to your bank account</message>
 
</goodnews>  

 

And this is the script:

# !/bin/bash
# Written by rafazzevedo
# http://www.azevedorafaela.wordpress.com

# Uncomment this if you want to use parameters given by #the user and change the variables for the parameters #position like: $1 is the first parameter, $2 is the #second. Instead of tag, xml_file...
# if [ $# -ne 3 ]; then
# echo 1>&2 "Please, use the parameters file, tag and new value."
# exit 127
# fi

xml_file="goodnews.xml"
tag="amount"
new_value="\$1000,000.00"

# We will create a temporary file, just to not modify directly the original one.
temporary="temp_file.temp"

# This space is just to identify the end of the xml.
echo " ">> $xml_file

# Extracting the value from the <$tag> element
tag_value=$(grep "<$tag>.*<.$tag>" $xml_file | sed -e "s/^.*<$tag/<$tag/" | cut -f2 -d">"| cut -f1 -d"<")

echo "Found tag value $tag_value..."

# Replacing element value with $new_value
sed -e "s/<$tag>$tag_value<\/$tag>/<$tag>$new_value<\/$tag>/g" $xml_file > $temporary

echo "Changing $tag to $new_value..."

# Updating the changes to the original file ($xml_file)
chmod 666 $xml_file
mv $temporary $xml_file

Thank you guys ! See you soon 🙂

Source: http://www.dotkam.com/2007/04/04/sed-to-parse-and-modify-xml-element-nodes/

http://stackoverflow.com/questions/19951369/how-to-store-grep-command-result-in-some-variable-in-shell

Create xml with shell script

Hi guys,

Today I will post a shell script that I am using to test a webservice. If you need a xml to test the performance of the server/webservice with Jmeter or if you just need generate a lot of xmls to make any type of test, this example can be useful.

In this code you can generate xmls and change some tags as id, date, etc…

#!/bin/bash
# Written by rafazzevedo
# http://www.azevedorafaela.wordpress.com

defaultpath="/User/" #Change this for the path where you want the files stay

echo "What do you want generate ?"
read element

echo "How many files ?"
read filescount
#Creating the subdirectory for each element and removing the existing folder
if [ ! -d $element ]
then
mkdir "$element"
else
rm -r "$element"
mkdir "$element"
fi

defaultpath=$defaultpath$element/

dateformat=$(date +"%m/%d/%YT%H:%M:%S")

for (( i=0;i<$filescount;i++ )) do

exec 3>temp.xml #Creating temporary file

#HEADER (Common for all xmls)
echo '&3'
echo ' xmlns:cpi="http://xml.wordpress.co.uk/types" >' >&3
echo " http://localhost:9999/$element$i" >&3
echo ' Title' >&3
echo " $dateformat" >&3
echo ' ' >&3
echo ' Rafaela Azevedo' >&3
echo ' http://wordpress.com/img.png' >&3
echo ' rafazzevedo@gmail.co.uk' >&3
echo " " >&3

#SPECIFIC TAGS OF EACH ELEMENT
case $element in

'article')
echo ' Content Test' >&3
echo ' ' >&3
;;

'image')
echo '' >&3
echo ' http://weather.gov/images/xml_logo.gif' >&3
echo ' NOAAs National Weather Service' >&3
echo ' http://weather.gov' >&3
echo '' >&3
;;

esac

echo '' >&3
cp ./temp.xml ${element}/$element-$i.xml #Copying the temporary files
rm ./temp.xml #Removing the temp files

done

echo "Created xml files"


It is helping me a lot lately , I do not need spend a lot of time doing some complex code or generate these files manually. I hope this helps you too .

If you have any suggestion or question, write a comment bellow 🙂

Thank you guys !