I am doing a mixed language script with the parent script being bash (don't ask why, it's a long story). Part of my script pulls the source of an XML page into a variable. I want to use bash to process the XML in the variable into several arrays. The XML is set up as follows:
<event>
<id>34287352</id>
<what>New Post</what>
<when>1 Minute Ago 03:50 PM</when>
<title>This is a title</title>
<preview>sdfasd</preview>
<poster>
<![CDATA[ USERNAME ]]>
</poster>
<threadid>2346566</threadid>
<postid>34287352</postid>
<lastpost>1360021837</lastpost>
<userid>3291696</userid>
<forumid>2</forumid>
<forumname>General Discussion</forumname>
<views>201,913</views>
<replies>6,709</replies>
<statusicon>images/statusicon/thread.gif</statusicon>
</event>
There are 20 <event>'s in the XML file. I want to pull what title and preview from the XML and put them all into their own array
I followed an example here on SOF
for tag in what title preview
do
OUT=`grep $tag $source | 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
W_ARRAY=( `echo ${what}` )
T_ARRAY=( `echo ${title}` )
P_ARRAY=( `echo ${preview}` )
echo ${W_ARRAY[0]}
echo ${T_ARRAY[0]}
echo ${P_ARRAY[0]}
But using the above my script always freaks right out and repeats grep: <part of the xml>: No such file or directory
Thoughts?
EDIT:
Well it is ugly as hell but I managed to get the sudoxml into an array
windex=0
tindex=0
pindex=0
while read -r line
do
WHAT=$(echo ${line} | awk -F "</?what>" '{ print $2 }')
if [ "$WHAT" != "" ]; then
W_ARRAY[$windex]=$OUT
let windex+=1
fi
TITLE=$(echo ${line} | awk -F "</?title>" '{ print $2 }')
if [ "$TITLE" != "" ]; then
T_ARRAY[$tindex]=$OUT
let tindex+=1
fi
PREVIEW=$(echo ${line} | awk -F "</?preview>" '{ print $2 }')
if [ "$PREVIEW" != "" ]; then
P_ARRAY[$pindex]=$OUT
let pindex+=1
fi
done <<< "$source"
$sourcevariable set ?