Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How do I get the text value of an element using XML Parser?

by krujos (Curate)
on Jan 26, 2002 at 07:37 UTC ( #141719=perlquestion: print w/ replies, xml ) Need Help??
krujos has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to get the value between the elements and I am a little confused how to do this so far I have this to start my parser
#!/usr/bin/perl use strict; use warnings; use CGI qw ( :standard ); use Fcntl qw ( :flock ); use XML::Parser; if (param) { my ($parser, $document, $users); open(USERS, "../../xml_final/users/users.xml") || die "Could not open users.xml $!"; flock(USERS, LOCK_SH); print header; print start_html; $parser= new XML::Parser(); $parser->setHandlers( Start => \&start_handler, #End => \&end_handler, $document = $parser->parse (\*USERS); close (USERS); sub start_handler{ my ($expat, $element, %attributes) =@_; my ($flag); $flag=0; if ($flag==0 && $element eq param("username")) { $flag=1; } if ($flag==1 && $element eq param("password")) { print p("Good Job"); } }
what I am trying to do is get the values between the elements named username and password in the xml document which looks like this.
<?xml version ="1.0"?> <users> <user> <name>Joshua</name> <username>user</username> <password>pass</password> </user> </users>

Comment on How do I get the text value of an element using XML Parser?
Select or Download Code
Re: How do I get the text value of an element using XML Parser?
by Anonymous Monk on Jan 26, 2002 at 08:01 UTC
    ya gots to set up another handler, like the XML::Parser Tutorial demonstrates ;D
    #!/usr/bin/perl use strict; use warnings; use CGI qw ( :standard ); use Fcntl qw ( :flock ); use XML::Parser; use vars / $flag /; if (param) { my ($parser, $document, $users); open(USERS, "../../xml_final/users/users.xml") || die "Could not open users.xml $!"; flock(USERS, LOCK_SH); print header; print start_html; $parser= new XML::Parser(); $parser->setHandlers( Start => \&start_handler, End => \&end_handler, Char => \&char_handler, ); $document = $parser->parse (\*USERS); close (USERS); sub char_handler{ my ($expat, $text) =@_; $main::username .= $text if $flag == 0; $main::password .= $text if $flag == 1; } sub start_handler{ my ($expat, $element, %attributes) =@_; if ($flag==0 && $element eq param("username")) { $flag=1; } if ($flag==1 && $element eq param("password")) { print p("Good Job"); } }
Re: How do I get the text value of an element using XML Parser?
by mirod (Canon) on Jan 26, 2002 at 13:51 UTC

    Actually your best bet might be not to use XML::Parser at all, but to use a higher level (read easier-to-use) module.

    If your data is as simple as what you describe and the file is small enough that you can load it all in memory, which is likely, you can use XML::Simple:

    #!/bin/perl -w use strict; use XML::Simple; # depends on XML::Parser use Data::Denter; # just to check what's read in by my $data= XMLin( \*DATA); # read the data, you would use "./$file" print Denter( $data), "\n"; # just checking __DATA__ <users> <user> <name>Joshua Kruck</name><username>krujos</username><password>jonn +y5</password> </user> <user> <name>John Doe</name><username>jdoe</username><password>jane</pass +word> </user> </users>

    Will give you the following output:

    % user => % John Doe => % password => jane username => jdoe Joshua Kruck => % password => jonny5 username => krujos

    Read the docs as for example you might want the keys to the hash to be the username and not the name (use keyattr => 'username' when you call XMLin).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://141719]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (10)
As of 2014-07-22 14:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (115 votes), past polls