[ Date Index ][
Thread Index ]
[ <= Previous by date / thread ] [ Next by date / thread => ]
On Sun, 2003-10-05 at 11:44, Neil Williams wrote: > Does anyone here use XML::Parser in Perl? > > I'm getting confused over the constant use of references to parse the tree and > I can't get any data from a sample XML. > > <?xml version="1.0"?> > <!DOCTYPE ISBNORG SYSTEM "http://www.isbn.org.uk/xml/isbnorg.dtd"> > <ISBNORG> > <RECORD> > <ISBN>1565922867</ISBN> > <AUTHOR>Ellen Siever, Stephen Spainhour & Nathan Patwardhan.</AUTHOR> > <FULLTITLE>Perl in a nutshell : a desktop quick reference / Ellen Siever, > Stephen Spainhour & Nathan Patwardhan.</FULLTITLE> > <SHORTTITLE>Perl in a nutshell :</SHORTTITLE> > <EDITION></EDITION> > <PUBLISHER>O'Reilly,</PUBLISHER> > <DATE>c1999.</DATE> > <SUBJECT>Perl (Computer program language)</SUBJECT> > </RECORD> > </ISBNORG> > > I can work it in PHP but I want to try Perl. > > Could anyone help with an example script - I can't make sense of the POD > output, it just talks of references and nodes, there's no example code for > actually processing the nodes. You might want to look at XML::Simple, which is built on top of XML:Parser.As the name implies it doesn't do that much, but it makes what it does trivial. It uses a simple function to read in the data: XMLin([<xml file or string>] [, <options>]); which can then be read as a hashreference. It also has an XMLOut function that does the same in reverse Paul M example script follows: #! /usr/bin/perl -w use Data::Dumper; use XML::Simple; #read in record my $bookxml = XMLin("isbnorg.xml"); # access values (really simple) print "Author:".$bookxml->{RECORD}->{AUTHOR}."\n"; print "Title:".$bookxml->{RECORD}->{SHORTTITLE}."\n"; print "ISBN:".$bookxml->{RECORD}->{ISBN}."\n"; # use Data::Dumper to print out data print Dumper($bookout); # use hash ref to create new XML record my $bookout=XMLout({ 'RECORD'=> { 'PUBLISHER' => 'Jonathon Cape', 'FULLTITLE' => 'Hard Water / Jean Sprackland', 'AUTHOR' => 'Jean Sprackland', 'ISBN' => 022406594', 'DATE' => '2003', 'SHORTTITLE' => 'Hard Water', 'SUBJECT' => 'Poetry', 'EDITION' => '' } } , XMLDecl =>"<?xml version=\"1.0\"?><!DOCTYPE ISBNORG SYSTEM \"http://www.isbn.org.uk/xml/isbnorg.dtd\">", ROOTNAME=>"ISBNORG", NoAttr => 1); # N.B. Options above # XMLDecl sets XML declaration as "string" (used here to get the doctype in as well) # Rootname ensures root is ISBNORG (default is opt) # NoAttr => 1 is used to make xml come out as <name>value</name> rather than <name atrribute= "value"> # show we have got the data in my $bookxml2 = XMLin("$bookout"); print "Author:".$bookxml2->{RECORD}->{AUTHOR}."\n"; print "Title:".$bookxml->{RECORD}->{SHORTTITLE}."\n"; print "ISBN:".$bookxml->{RECORD}->{ISBN}."\n"; # use Data::Dumper to print out XML print Dumper($bookout); -- The Mailing List for the Devon & Cornwall LUG Mail majordomo@xxxxxxxxxxxx with "unsubscribe list" in the message body to unsubscribe.