Parsing XML

I am trying to parse an xml file and trying to grab certain values and inserting them into database table. I have the following xml that I am parsing:

<dd:service name="locator" link="false">
<dd:activation mode="manual" />
<dd:run mode="direct_persistent" proxified="false" managed="true" authenticated="false" perflog="false" dynlog="true" />
<dd:endpoint protocol="iiop" port="21120" />
</dd:service>
<dd:service name="node_daemon" link="false">
<dd:activation mode="manual" />
<dd:run mode="direct_persistent" proxified="false" managed="true" authenticated="false" perflog="false" dynlog="true" />
<dd:endpoint protocol="iiop" port="21121" />
</dd:service>
<dd:service name="management" link="false">
<dd:activation mode="manual" />
<dd:run mode="direct_persistent" proxified="false" managed="false" authenticated="false" perflog="false" dynlog="false" />
<dd:endpoint protocol="http" port="21122" />
<dd:endpoint protocol="iiop" port="21123" />
</dd:service>

I need to create a row for each service type. For instance I need to insert into a table for the service stanza:

locator, 21120
node_daemon, 21121
management, 21123

So I would like to grab the service type and iiop port, but I am having some difficulty doing so. I have come up with the following, but it isn't correct. This will only grab the port, but not the iiop port, but including http ports.

for k in `cat test.xml | grep port | awk -F"\"" '{print $4}'`
do
echo "$server,dev$i,test,$k" >> test.txt
done
fi

Hi, would awk be OK too? Try this:

awk -F\" '$1~"service name" {s=$2} $2=="iiop"{print s", "$4}' infile

If the format varies, you would need something a bit more complicated..

Works great, thanks a ton.

Its skipping the first stanza for some reason:

<dd:service name="locator" link="false">
<dd:activation mode="manual" />
<dd:run mode="direct_persistent" proxified="false" managed="true" authenticated="false" perflog="false" dynlog="true" />
<dd:endpoint protocol="iiop" port="20000" />
</dd:service>

It parses the other 4 correctly and my script inserts into the database correctly as well.

With your sample, I get:

locator, 21120
node_daemon, 21121
management, 21123

What do you get?

Got it to work. Thanks.

The awk is failing on solaris. Can someone help me rewrite this to run on solaris? This only works on Linux.

On Solaris use /usr/xpg4/bin/awk rather than awk