in reply to How do I get the text value of an element using XML Parser?
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).
|
|---|