Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML::Parser::Style::Subs

Status
Not open for further replies.

andyros

Programmer
Jul 12, 2005
42
GB
Hi all

just a quick question. I've been taking a look at the XML::parser module, its been a while since i did!

The subs style option seems intresting, when the parser hits an xml tag for example <bookname> it calls a sub routine you define as bookname.

If i were to implement this, is there any way to get the value of the <bookname> tag?

I've tried:

Code:
  use XML::Parser;
  my $p = XML::Parser->new(Style => 'Subs', Pkg => 'MySubs');

  $p->parsefile('test.xml');
  
  {
    package MySubs;
    
    sub bookname {
      print "start of bookname tag\n";
	  print "$_\n";
    }
    
    sub bookname_ {
      print "end of bookname\n";
    }
  }

the xml is

Code:
<book>
  <bookname>Harry Potter</bookname>
</book>

anyknow how to grab 'Harry Potter' from inside the subroutine?

thanks
andy
 
Hi trojan

the article doesnt seem to show how you get the element inside a tag, it only seems to establish the tag names?

Also he uses the start_handler which i am trying to avoid using as i dont want to take in the entire xml document and then work on it, i only want to grab specific data, like 'harry potter' for example.

I might be mistaken in this as the article seems advance in its use of hashes which im quite sketchy on, i opt for arrays!!

andy
 
Hashes and arrays are almost the same (from the point of view of usage). Both are collections of data and the only difference is how you index an element. With an array, it's a numeric index in sequence starting from zero. With a hash, you can use any index you like! You can use numeric (although an array would be more efficient) or a text index (or key as we call it when dealing with hashes).
Hopefully that explains arrays and hashes for you.
Now, with respect to the xml issue, I didn't stop to really look or read the page fully but it does appear that you need to regster a "char" handler to pick up the data you want.
I guess it would collect ALL char data but maybe you could flag when to start and stop with your "bookname" handlers.
Just a thought.


Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top