Quantcast
Viewing all articles
Browse latest Browse all 3

Perl script to parse XML using XML::LibXML;

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/perluse 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";}

Viewing all articles
Browse latest Browse all 3

Trending Articles