in reply to Extracting tagged data from a XML file

Using the proper tools (an XML parser, not regular expressions), it becomes pretty simple:

use XML::XPath; my $xp = XML::XPath->new(filename => '/path/to/file'); foreach ('IP-ADDRESS', 'IP-NEIGHBOUR') { foreach my $ip ($xp->findnodes("//$_")) { print $ip->findvalue('.'), "\n"; } }
--
<http://www.dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re^2: Extracting tagged data from a XML file
by theroninwins (Friar) on Aug 31, 2004 at 10:30 UTC
    I searched for XPath since i don't have it on my comp and i found a long read but how can i get it to work?? (same with XML::Simple.pm although i got that pm file but it is not really working)???
        Thanks for the link the prob is only that i am working from the office and here they don't have formats like tar and I don't have any Linux station next to me (sadly)so there is the prob (especially because I don't even have WinRaR or something like that)
Re: Extracting tagged data from a XML file
by theroninwins (Friar) on Aug 31, 2004 at 10:45 UTC
    The problem is still the same, it reads both the one taged "<IPAddress>" and the ones taged "<NEIGHBOURIPAddress>".
    148.192.116.253
    148.192.116.253
    148.192.116.253
    148.192.116.253
    148.192.116.253
    148.192.137.253
    148.192.137.253
    148.192.137.253
    148.192.137.253
    148.192.137.253
    148.192.137.253
    148.192.137.253
    148.192.137.253
    148.192.137.253
    148.192.137.253
    148.192.137.253
    148.192.137.253
    148.192.137.253
    148.192.137.253
    148.192.137.253
    148.192.137.253
    148.192.63.249
    148.192.63.249
    148.192.63.249
    148.192.63.249
    148.192.63.249
    148.192.63.249
    148.192.63.249
    148.192.63.249
    148.192.63.249
    148.192.63.249
    this is what sonme of them look like! many are the same. :-(

      It looks like that's what you asked for. I'm not sure what the problem is.

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

        The problem is that i need every IP only once. (Sorry if i didn't say that before). So i only need to get the IPAddress and not also the NEIGHBOURIPAddress, bacause then i have everything only once.
      ok here is another idea I had:

      # use XML::Dumper; open (XMLINPUT, "e:/topo.xml") or (print "Die XML Datei konnte nicht geöffnet werden!!\n". "Bitte Pfad und Dateiname mit Slashs angeben!!". "\nz.B. c:/data1.xml\n" and open (MACERROR, ">XMLError.err") and print MACERROR "Die XML Datei konnte nicht geöffnet werden!!\n +". "Bitte Pfad und Dateiname mit Slashs angeben!!". "\nz.B. c:/data1.xml\n" and close MACERROR and exit); # Zeiger erzeugen $zeiger_xml = new XML::Dumper->xml2pl(join("",<XMLINPUT>)); $zeiger_data = \@{$zeiger_xml->{'Data'}}; close XMLINPUT; open (CACHENEU, ">ips2.txt"); close CACHENEU; foreach $z_device (@{$zeiger_xml->{'Device'}}) { #if ( $z_device->{'SystemDescription'} =~ m/Version 12.0 +\(5\)/ #and $z_device->{'SystemDescription'} =~ m/C2900/ #and $z_device->{'SystemDescription'} !~ m/WS/) #{ open (CACHENEU, ">>ips2.txt"); foreach $z_add (@{$z_device->{'DeviceName'}}) { %PortHash = ( $z_device->{'DeviceName'} => $z_ip->{'IPAddr +ess'} ); printf CACHENEU ("%s\t\t%s\n",%PortHash); } close CACHENEU; #} } print "\nfertig Portliste erstellen!!\n" ;


      and this is the File I have to read from:

      <?xml version="1.0" encoding="UTF-8" ?> - <CMData> <CMServer>S03</CMServer> <CreatedAt>Tue Aug 24 09:09:41 GMT+02:00 2004</CreatedAt> <SchemaVersion>1.0</SchemaVersion> <Heading>Topology Data</Heading> - <Layer2Details> - <Device> <DeviceName>LWL-H91-CW-4-5-4</DeviceName> <IPAddress>148.192.59.254</IPAddress> <DeviceState>Reachable</DeviceState> <DeviceType>C2950G-24</DeviceType> - <Neighbors> - <Neighbor> <NeighborIPAddress>148.192.22.22</NeighborIPAddress> <NeighborDeviceType>C6506</NeighborDeviceType> <Link>Point to Point link</Link> <LocalPort>Gi0/2</LocalPort> <RemotePort>3/3</RemotePort> </Neighbor>


      Any idea on where I am mistaking??