How to parse the following xml file

Hi,

I have the following file

Example.xml

<?xml version="1.0" encoding="iso-8859-1"?>

<html><set label="09/07/29" value="1241.90"/>

</html>

Can any one help me in parsing this xml file

I want to retrive the attribute values of the tag set

Example I want to store 09/07/29 to variable1 and 1241.90 to variable2 in order to insert this data into a database table.

Appreciate if any one can help me..very urgent

this might not be the best way, but you could try:

variable1=`perl -nl -e 'if (m/label=\"(.*?)\"/) { print $1;} ' newfile.txt`
variable2=`perl -nl -e 'if (m/value=\"(.*?)\"/) { print $1;} ' newfile.txt`

I think it is a good way :slight_smile:

@Raji_gadam
Please use CODE-tags next time you post code, data or logs to enhance readability and to preserve formatting like indention etc., ty.

Just for variety with sed:

LABEL=`sed -n 's/.*label="\([^\"]*\)".*/\1/p' infile`
VALUE=`sed -n 's/.*value="\([^\"]*\)".*/\1/p' infile`

try using xmllint and xpath ... you can extract exactly what you want..
example: the file is called t.xml

echo 'cat /html/set/@label' | xmllint --shell t.xml | grep '='

will get you label

echo 'cat /html/set/@value' | xmllint --shell t.xml | grep '='

will get you set ...
Now pipe to awk or sed and extract what you want ...
This will work with any size XML ..

1 Like