develooper Front page | perl.beginners | Postings from February 2002

Re: PERL and XML Parser

Thread Previous | Thread Next
From:
P0R0NG
Date:
February 1, 2002 08:16
Subject:
Re: PERL and XML Parser
Message ID:
20020202002901.5cb242c9.p0r0ng@yahoo.com
oh yes thanks for that! thanks. i overlooked that. my big...

anyways, here's the correct xml version:

<? xml version="1.0" standalone="yes" ?>
<albums>
  <owner>Percy</owner>
  <category name ="rock">
    <artist>Alice in Chains</artist>
    <artist>The Pixies</artist>
    <artist>Percy and the Test Tube Babies</artist>
  </category>  
  <category name = "reggae">
    <artist>Bob Marley</artist>
    <artist>Peter Tosh</artist>
    <artist>Percy Gone Jazz</artist>
  </category>  
</albums>

in using xml parser, how can i get the content of element 'owner'? i
created the code but every content between the xml elements we parsed.
here's my code:

#! /usr/local/bin/perl

use XML::Parser;
my $parser = new XML::Parser ();

$parser->setHandlers (Start => \&Start_handler,
                      End => \&End_handler,
                      Default => \&Default_handler
                     );

my $filename = shift;
die "Can't find '$filename': $!\n" unless -f $filename;

$parser->parsefile ($filename);

sub Start_handler {
  my $p  = shift;
  my $el = shift;

  if ($el =~ m/owner/g) {print "<$el>"};
}

sub End_handler {
  my ($p,$el) = @_;
  if ($el =~ m/owner/g) { print "</$el>\n"; };
}

sub Default_handler {
  my ($p,$str) = @_;
  # my $p = shift;
  # my $str = shift;
  if (($str =~m/Percy/g) or ($str =~m/Percy/g)){ print "\n$str\n"; }
}

On Fri, 1 Feb 2002 10:27:37 -0500 
"Hanson, Robert" <RHanson@APTEGRITY.com> wrote:

|By the way, there were a lot of XML errors in that example.
|
|<categories="rock">
|
|You need an attribute name, this is not legal.
|
|<artist>Alice in Chains</rock>
|
|The closing tag does not match the opening tag.
|
|<categories = "reggae">
|
|You need an attribute name, this is not legal.
|
|Rob
|
|
|-----Original Message-----
|From: P0R0NG [mailto:p0r0ng@yahoo.com]
|Sent: Friday, February 01, 2002 8:05 AM
|To: beginners@perl.org
|Subject: PERL and XML Parser
|
|
|Hi list.
|
|I'm currently doing a perl project involving XML parser.
|
|given this xml snippet:
|
|<albums>
|  <owner>Percy</owner>
|  <categories="rock">
|    <artist>Alice in Chains</rock>
|    <artist>The Pixies</artist>
|  </categories>  
|  <categories = "reggae">
|    <artist>Bob Marley</artist>
|    <artist>Peter Tosh</artist>
|  </categories>  
|</albums>

_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About