AlexPham AlexPham - 2 years ago 86
Perl Question

XML::Simple in perl, with mixed xml file

I have an DEMO.xml file look like this.

<?xml version="1.0"?>
<pattern owners="alex">heloworld</pattern>
<pattern owners="alex">perlprogramming</pattern>

This is my perl code to parse this file:

use XML::Simple;
use strict;
use Data::Dumper;

my $xml = new XML::Simple;
my $data = $xml->XMLin("DEMO.xml");
print Dumper($data);

And here is what I got:

$VAR1 = {
'pattern' => [
'owners' => 'alex',
'content' => 'heloworld'
'owners' => 'alex',
'content' => 'perlprogramming'

May I know if I just simply print out the content of xml tag, what should I do ?

The expected output might look like:


Thank you so much!


Answer Source

Rule one of using XML::Simple: Don't, use something else.

I like XML::Twig and your problem looks like this:


use strict;
use warnings;

use XML::Twig;

my $twig = XML::Twig -> new -> parsefile('DEMO.xml'); 
foreach my $pattern ( $twig -> get_xpath ('//pattern') ) {
   print $pattern -> text,"\n";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download