XML to Text file Parsing Using shell scripting

Hi folks,
Need some help with XML to text file parsing , the following is the content of the XML File.

<xn:SubNetwork id="SNJNPRZDCR0R03">
                <xn:MeContext id="PRSJU0005">
                    <xn:VsDataContainer id="PRSJU0005">
                        <xn:attributes>
                            <xn:vsDataType>vsDataMeContext</xn:vsDataType>
                            <xn:vsDataFormatVersion>EricssonSpecificAttributes.11.03</xn:vsDataFormatVersion>
                            <es:vsDataMeContext>
                                <es:userLabel>PRSJU0005</es:userLabel>
                                <es:ipAddress>166.206.252.42</es:ipAddress>
                                <es:neMIMversion>vN.1.4</es:neMIMversion>
                                <es:lostSynchronisation>SYNCHRONISED</es:lostSynchronisation>
                                <es:rbsIubId>214</es:rbsIubId>
                                <es:multiStandardRbs6k>false</es:multiStandardRbs6k>
                                <es:mirrorMIBversion>N.1.12.K.1.3</es:mirrorMIBversion>
                            </es:vsDataMeContext>
                        </xn:attributes>
                    </xn:VsDataContainer>
                </xn:MeContext>
    </xn:SubNetwork>
<xn:SubNetwork id="LSVLKY53CR0R02">
                <xn:MeContext id="KYLSU1005">
                    <xn:VsDataContainer id="KYLSU1005">
                        <xn:attributes>
                            <xn:vsDataType>vsDataMeContext</xn:vsDataType>
                            <xn:vsDataFormatVersion>EricssonSpecificAttributes.11.03</xn:vsDataFormatVersion>
                            <es:vsDataMeContext>
                                <es:userLabel>KYLSU1005</es:userLabel>
                                <es:ipAddress>107.71.129.250</es:ipAddress>
                                <es:neMIMversion>vN.1.4</es:neMIMversion>
                                <es:lostSynchronisation>SYNCHRONISED</es:lostSynchronisation>
                                <es:rbsIubId>1005</es:rbsIubId>
                                <es:multiStandardRbs6k>false</es:multiStandardRbs6k>
                                <es:mirrorMIBversion>N.1.12.K.1.3</es:mirrorMIBversion>
                            </es:vsDataMeContext>
                        </xn:attributes>
                    </xn:VsDataContainer>
                </xn:MeContext>
    </xn:SubNetwork>

The Desired Output should be in a text file eg:

SubNetwork id    MeContext id    ipAddress
SNJNPRZDCR0R03    PRSJU0005    166.206.252.42
LSVLKY53CR0R02    KYLSU1005    107.71.129.250

Any Help would be greatly Appreciated.

Do you want just three fields from the entire file? Based on the sample file u have given, you have picked up only Subnet, Context and IP Address.

yes losttouch, i only want those three fields.

Thanks//

Install perl XML::XPath module

# yum install perl-XML-XPath.noarch
or
# perl -MCPAN -e 'install XML::XPath'

Try this:

#! /usr/bin/perl
use XML::XPath;
$,=" ";

$xp=XML::XPath->new($ARGV[0]);
$nodes=$xp->find('//xn:SubNetwork');
foreach $i ($nodes->get_nodelist) {
        $sn=$i->getAttribute("id");
        foreach $j ($i->find('xn:MeContext')->get_nodelist) {
                $mc=$j->getAttribute("id");
                foreach $k ($j->find('xn:VsDataContainer/xn:attributes/es:vsDataMeContext/es:ipAddress/text()')->get_nodelist) {
                        print $sn, $mc, $k->getNodeValue(),"\n";
                }
        }
}
1 Like

try xmllint

check out
openSUSE Lizards

Have a look at section under "Using Namespaces in XPath Expressions"

nawk -F"[\"\>\<]" 'BEGIN{print "SubNetwork id    MeContext id    ipAddress"} /SubNetwork id/{a=$3} /userLabel/{b=$3}/ipAddress/{print a,b,$3}' input.txt

Thank you itkamaraj , the command worked like a charm.