Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl script to parse XML using XML::LibXML;

Tags:

xml

perl

libxml2

I think this is a very simple issue, but I cannot figure it out despite many searches.

I am trying to parse the following XML to print something similar to TAG=VALUE, so that I can write this to a CSV file. The problem is the tags are not always the same for each sample. I cannot seem to figure out how to get the actual tag names. Any help appreciated!!!

XML File -

<Statistics>
  <Stats>
    <Sample>
        <Name>System1</Name>
        <Type>IBM</Type>
        <Memory>2GB</Memory>
        <StartTime>2012-04-26T14:30:01Z</StartTime>
        <EndTime>2012-04-26T14:45:01Z</EndTime>
    </Sample>

    <Sample>
        <Name>System2</Name>
        <Type>Intel</Type>
        <Disks>2</Disks>
        <StartTime>2012-04-26T15:30:01Z</StartTime>
        <EndTime>2012-04-26T15:45:01Z</EndTime>
        <Video>1</Video>
    </Sample>
  </Stats>
</Statistics>

Script -

#!/usr/bin/perl
use XML::LibXML;

$filename = "data.xml";

my $parser = XML::LibXML->new();
my $xmldoc = $parser->parse_file($filename);

for my $sample ($xmldoc->findnodes('/Statistics/Stats/Sample')) {

print $sample->nodeName(), ": ", $sample->textContent(), "\n";

}
like image 653
lozwell Avatar asked May 01 '12 20:05

lozwell


People also ask

How do I read an XML file in Perl?

If you have the XML in a string, instead of location , use string : $dom = XML::LibXML->load_xml(string => $xml_string); Or, you can provide a Perl file handle to parse from an open file or socket, using IO : $dom = XML::LibXML->load_xml(IO => $fh);

How do I create an XML file in Perl?

Creating XML can be done just as easily as creating HTML, using the XML::Writer module, and its CGI.pm- like interface, from a relational table, using DBIx-XML-RDB (don't you love those names?) or from a Web form using XML::CGI.

How install XML module Perl?

The easiest is to visit the author's CPAN directory. Links to author directories can be found in the Author section below. An easier method is to use the CPAN module, i.e.: perl -MCPAN -e shell 'install XML::Parser' which installs the module automagically.


1 Answers

You have the right method for getting the tag names, you just need an extra loop to run through the tags inside each <sample>:

#!/usr/bin/perl

use strict;
use warnings;

use XML::LibXML;

my $filename = "data.xml";

my $parser = XML::LibXML->new();
my $xmldoc = $parser->parse_file($filename);

for my $sample ($xmldoc->findnodes('/Statistics/Stats/Sample')) {
    for my $property ($sample->findnodes('./*')) {
        print $property->nodeName(), ": ", $property->textContent(), "\n";
    }
    print "\n";
}

Edit: I have now created a tutorial site called Perl XML::LibXML by Example which answers exactly this type of question.

like image 105
Grant McLean Avatar answered Sep 19 '22 22:09

Grant McLean