Re: PERL and XML Parser
On Fri, 1 Feb 2002 21:04:54 +0800 P0R0NG [EMAIL PROTECTED] wrote: |how can i get the PCDATA for the element owner? i want to get just |the owner elements and the content of the owner. | |i've created a code that will trap for the start and end tags of |the element owner as well as the handlers for the characters |that were part of the markup but no particular handlers. i forgot, my code was getting all the contents with no particular handlers. (ie contents of rock, reggae, owner elements were trapped. all i want is the content of the owner elements. thanks again. percy _ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: PERL and XML Parser
There are tons of XML modules, many of which make that sort of thing easy... but it depends on exactly what you want to do. Here are some snippets: use XML::EasyOBJ; my $obj = new XML::EasyOBJ('killme.xml'); foreach ( $obj-albums ) { print $_-owner-getString . \n; } -- OR -- use XML::RAX; my $rax = new XML::RAX; $rax-openfile('killme.xml'); $rax-setRecord('albums'); while ( my $rec = $rax-readRecord ) { print $rec-getField('owner') . \n; } And there are *LOTS* more. It all depends on what you need to do. BTW - The current version of XML::EasyOBJ on CPAN is a bit old. I have a newer version that includes read and write capabilities but I haven't written up the new docs for it yet. I can send it if you wanted. Rob -Original Message- From: P0R0NG [mailto:[EMAIL PROTECTED]] Sent: Friday, February 01, 2002 8:10 AM To: [EMAIL PROTECTED] Subject: Re: PERL and XML Parser On Fri, 1 Feb 2002 21:04:54 +0800 P0R0NG [EMAIL PROTECTED] wrote: |how can i get the PCDATA for the element owner? i want to get just |the owner elements and the content of the owner. | |i've created a code that will trap for the start and end tags of |the element owner as well as the handlers for the characters |that were part of the markup but no particular handlers. i forgot, my code was getting all the contents with no particular handlers. (ie contents of rock, reggae, owner elements were trapped. all i want is the content of the owner elements. thanks again. percy -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: PERL and XML Parser
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 ownerPercy/owner category name =rock artistAlice in Chains/artist artistThe Pixies/artist artistPercy and the Test Tube Babies/artist /category category name = reggae artistBob Marley/artist artistPeter Tosh/artist artistPercy 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 [EMAIL PROTECTED] 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. | |artistAlice 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:[EMAIL PROTECTED]] |Sent: Friday, February 01, 2002 8:05 AM |To: [EMAIL PROTECTED] |Subject: PERL and XML Parser | | |Hi list. | |I'm currently doing a perl project involving XML parser. | |given this xml snippet: | |albums | ownerPercy/owner | categories=rock |artistAlice in Chains/rock |artistThe Pixies/artist | /categories | categories = reggae |artistBob Marley/artist |artistPeter Tosh/artist | /categories |/albums _ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: PERL and XML Parser
I not sure how more experienced Perl developers feel but in addition to XML::Parser, I found XML::SimpleObject a great way to start parsing XML. Here's a link: http://www.xml.com/pub/a/2001/04/18/perlxmlqstart1.html Additionally, you can model the data anyway you want, but here's a little twist on what you have that may be worth pondering: album artistAlice in Chains/artist ownerPercy/owner categoryRock/category /album album artistThe Pixies/artist ownerPercy/owner categoryRock/category /album album artistPercy and the Test Tube Babies/artist ownerPercy/owner categoryRock/category /album album artistBob Marley/artist ownerPercy/owner categoryReggae/category /album album artistPeter Tosh/artist ownerPercy/owner categoryReggae/category /album album artistPercy Gone Jazz/artist ownerPercy/owner categoryReggae/category /album Yes Percy (owner) is repeated and so is the category, but it makes it a little easier to add another album to your file: album artistNapalm Death/artist ownerJoel/owner categoryBallads/category /album Hope this helps. Joel -Original Message- From: P0R0NG [mailto:[EMAIL PROTECTED]] Sent: Friday, February 01, 2002 8:29 AM To: [EMAIL PROTECTED] Cc: Hanson, Robert Subject: Re: PERL and XML Parser 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 ownerPercy/owner category name =rock artistAlice in Chains/artist artistThe Pixies/artist artistPercy and the Test Tube Babies/artist /category category name = reggae artistBob Marley/artist artistPeter Tosh/artist artistPercy 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 [EMAIL PROTECTED] 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. | |artistAlice 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:[EMAIL PROTECTED]] |Sent: Friday, February 01, 2002 8:05 AM |To: [EMAIL PROTECTED] |Subject: PERL and XML Parser | | |Hi list. | |I'm currently doing a perl project involving XML parser. | |given this xml snippet: | |albums | ownerPercy/owner | categories=rock |artistAlice in Chains/rock |artistThe Pixies/artist | /categories | categories = reggae |artistBob Marley/artist |artistPeter Tosh/artist | /categories |/albums _ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]