This is a discussion on Parse XML file and store data in array in shell scripting within the Shell scripting forums, part of the Development/Scripting category; Hello, I have the XML file in the format <Users> <Host> <hostAddress>180.144.226.47</hostAddress> <userName>pwdfe</userName> <password>hjitre</password> <instanceCount>2</instanceCount> </Host> <Host> <hostAddress>180.144.226.87</hostAddress> <userName>trrrer</userName> <password>jhjjhhj</password> ...
|
|||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
|
|||
|
Hello,
I have the XML file in the format <Users> <Host> <hostAddress>180.144.226.47</hostAddress> <userName>pwdfe</userName> <password>hjitre</password> <instanceCount>2</instanceCount> </Host> <Host> <hostAddress>180.144.226.87</hostAddress> <userName>trrrer</userName> <password>jhjjhhj</password> <instanceCount>3</instanceCount> </Host> <Host> <hostAddress>180.455.226.87</hostAddress> <userName>wewqw</userName> <password>dfsdfd</password> <instanceCount>3</instanceCount> </Host> </Users> I have to read this xml file from the shell script and store the value of the tags hostAddress,username,password,instancecount in a separate arrays. Please help me out in solving this. |
| Sponsored Links | ||
|
|
|
|||
|
Code:
for tag in hostAddress username password instancecount
do
grep $tag in.xml | tr -d '\t' | sed 's/^<.*>\([^<].*\)<.*>$/\1/'
done
|
|
|||
|
Quote:
Code:
#!/bin/bash
for tag in hostAddress userName password instanceCount
do
OUT=`grep $tag in.xml | tr -d '\t' | sed 's/^<.*>\([^<].*\)<.*>$/\1/' `
# This is what I call the eval_trick, difficult to explain in words.
eval ${tag}=`echo -ne \""${OUT}"\"`
done
# So let's stuff the obtained results into 4 different Arrays
H_ARRAY=( `echo ${hostAddress}` )
U_ARRAY=( `echo ${userName}` )
P_ARRAY=( `echo ${password}` )
I_ARRAY=( `echo ${instanceCount}` )
# Ok, time to announce success, let's printout each of the arrays
echo ${H_ARRAY[@]}
echo ${U_ARRAY[@]}
echo ${P_ARRAY[@]}
echo ${I_ARRAY[@]}
# For the benefit of agn -
# We can now refer to each unique element of the array like this -
echo ${H_ARRAY[0]}
# The above prints the first item in array H_ARRAY
The specs look rather challenging, for my poor knowledge of sed. So let's see if agn can crack this one too! I want to create a list of web-sites that definitely contain pornographic, or adult content, that's not suitable for kids, at school. I can see that the dmoz offers it's data in an xml format. I also noticed that the xml file contains descriptive information about each web-site. Now this is what I want to do - A shell script, wherein I specify (via PCRE, of course) the look_up_string. Based on the look_up_string, I want to, collect in a file the names of web-sites. I don't want the whole URL, just the hostname is enough. I will then later set this hostname in my hosts file, to ensure effective blocking of these sites. Could anybody help on this? |
|
|||
|
Regex's are not a good tool to parse html/xml data. You should use an XML parser. Right tool for the right job. |
|
|||
|
Quote:
I've tried everything listed in freshmeat / sourceforge for "dmoz" parsing. Sadly, none of them really work, and all are poorly documented. Besides I feel like a retard when it comes to perl. |
|
|||
|
Am a total newbie to XML parsing. But, XML::Simple[1] looks easy.
[1] XML::Simple - Easy API to maintain XML (esp config file - search.cpan.org Last edited by agn; 02-14-2008 at 12:07 PM. |
![]() |
| Bookmarks |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Rearange Data from a file to another | sebastanov | Shell scripting | 1 | 04-16-2008 10:46 AM |
| need help on shell scripting | rahul_sayz | Shell scripting | 1 | 12-08-2007 10:37 AM |
| get data mysql from shell | alpa | Shell scripting | 1 | 05-14-2007 06:02 PM |
| Shell scripting - Removing file extension | urbanreformer | Shell scripting | 3 | 03-07-2007 08:44 PM |
| Learning Shell Scripting | ricc | Shell scripting | 4 | 08-30-2005 09:37 AM |