Perl Basics
Introduction to Perl
FAQ's
CGI
Regular Expressions

PC Overview
Cool Stuff
My Modules
Success Stories
Links
Perl in the News
Logos
Perl @ msn
Perl Chat

PC Internals
About
Contact
Guest Book
Handy Logos
What's new

XML::Parser

XML::Parser
See also xml3.htm
P-friendly

[ home ] - [ search ] - [ sitemap ]

use XML::Parser;
use Data::Dumper;

#####  GLOBAL
my $NR_OF_INNER_ATTRS = 2; # <first>, <last>
my @xmlData = ();
my $xmlString = &slurpFile($ARGV[0]);
my @attributes = ();


#####################################################################
##	MAIN
#####################################################################

print qq~
===== Input ====================================================================
${$xmlString}
=========================================================================
~;
my $p1 = new XML::Parser(Style => 'Tree'); # Debug || Tree || Objects

my @result = $p1->parse(${$xmlString});

print "=> $result[0][0]\:\n";

@xmlData = $result[0][1]; # first xml file



my $counter = 0;

#---------  get attribute captions ---------#
my $i = 0;
$i = 3; while (1) {
	last unless defined $xmlData[0][$i];
	$counter++;
	push(@attributes, $xmlData[0][$i]);
	$i += 4;
}

my $nr_of_records = $counter / $NR_OF_INNER_ATTRS;

print "$nr_of_records xml records found\n\n";

#---------  get attribute values ---------#
$counter = 0;
$i = 4; while (1) {
	last unless defined $xmlData[0][$i][2];

	my $captionIndex = $counter % $NR_OF_INNER_ATTRS;
	print "-------------------------------\n" unless $captionIndex; # start of a XML rec
	print $attributes[$captionIndex], ": \t", $xmlData[0][$i][2], "\n";
	$counter++;
	$i += 4;
}


########################################################################
#####  SUBS
########################################################################
sub slurpFile ($) {
	local $/ = undef;
	open(INCFILE, $_[0]) or print $!; my $data = <INCFILE>; close INCFILE;
	\$data;
}
__END__


Input

<?xml version="1.0" encoding="iso-8859-1" ?>

<result>
	/* first xml rec */

	<first>Reto</first>
	<last>Hersiczky</last>

	/* second xml rec */

	<first>Hans</first>
	<last>Muster</last>

	<first>Pino</first>
	<last>Calzo</last>

	<first>Anton</first>
	<last>Schaller</last>

	/* 8 tokens */
</result>

Output

===== Input ====================================================================
<?xml version="1.0" encoding="iso-8859-1" ?>

<result>
	/* first xml rec */

	<first>Reto</first>
	<last>Hersiczky</last>

	/* second xml rec */

	<first>Hans</first>
	<last>Muster</last>

	<first>Pino</first>
	<last>Calzo</last>

	<first>Anton</first>
	<last>Schaller</last>

	/* 8 tokens */
</result>

=========================================================================
=> result:
4 xml records found

-------------------------------
first: 	Reto
last: 	Hersiczky
-------------------------------
first: 	Hans
last: 	Muster
-------------------------------
first: 	Pino
last: 	Calzo
-------------------------------
first: 	Anton
last: 	Schaller
home - feedback - search - web jobs
© 1998-2004 retoh :)