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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Perl question

Status
Not open for further replies.

WebmasterA

Programmer
Mar 26, 2004
25
US
Hello,

I have never done xml before and now I need to start working with it.
I need to read an xml file, modify some data and attributes and write the file out.

My questions are:
What are the good perl modules to use?
Where can I get good examples?

I have searched cpan and some other places and I have found many modules like XML::parser and so on, but examples are not good enough. Most of them show only how to get data out. I was not able to find any examples to change the data (especially attributes).

Thank you.
 
Thank you for reply.

Well it helps a bit, but not too much.
My question is if I have XML file converter into the tree using XML::parser how do I change some of the data there and how do I write it back into xml file?
I hope there is a module for that. And any code samples are appreciated.

Thank you
 
Well As far as I know
XML::Simple allows you to read in XML files as nested hashes of references, which can be very useful if you're only extracting a few values, here is the link for that


And XML::parser is a SAX based parser. It requires a bit more work than the previous too to set up, as you need to set up event handlers for all of the tags encountered in parsing. As long as you don't need to walk a tree when parsing and can handle all of the tags as they come, this should be quite powerful, and very light on memory.




And finally, if you do need DOM, theres XML::DOM (which is built on top of XML::parser). I haven't used it myself, but it can be found here:

Here's a very bare-bones example. But it should give you some idea.

Assuming the password file is in this format:

<passwords>
<user>
<name>happy</name>
<pass>sad</pass>
</user>
<user>
<name>dopey</name>
<pass>smart</pass>
</user>
<user>
<name>doc</name>
<pass>patient</pass>
</user>
<user>
<name>sneezy</name>
<pass>chirpy</pass>
</user>
<user>
<name>bashful</name>
<pass>noway</pass>
</user>
</passwords>


Here's the script. Run it with three arguments: password file name, user name and password to check for.

#!/usr/local/bin/perl -w

use XML::parser;

my ($file, $input_user, $input_pass) = @ARGV;

$xp = new XML::parser(Handlers => {Start => \&start_handler,
End => \&end_handler,
Char => \&char_handler});

die "can't create XML::parser object; $!\n"
unless $xp;

my $found = undef;
$xp->parsefile($file);

if ($found) {
print "Welcome, $input_user!\n";
} else {
print "Sorry, $input_user!\n";
}

sub start_handler
{
my ($xp, $elem) = @_;

# print "Start: $elem\n";
if ($elem eq 'name') {
$user_seen = 1;
} elsif ($elem eq 'pass') {
$pass_seen = 1;
}
}

sub end_handler
{
my ($xp, $elem) = @_;

# print "End: $elem\n";
if ($elem eq 'name') {
$user_seen = 0;
} elsif ($elem eq 'pass') {
$pass_seen = 0;
}
}

sub char_handler
{
my ($xp, $str) = @_;

# print " Value: $str\n";
if ($user_seen) {
$curr_user = $str;
} elsif ($pass_seen) {
$curr_pass = $str;
check_pass($curr_user, $curr_pass)
unless ($found);
}
}

sub check_pass
{
my ($user, $pass) = @_;

# print "Checking $user/$pass against $input_user/$input_pass ...\n";
$found = $user eq $input_user and $pass eq $input_pass;
}



Hope that helps.
 
Thank you very much guys.
I will read articles and try example and let you know how it worked out.
 
Hello,

OK I have looked and I have read a lot of stuff but I still not able to make whatever I need work.

Here is the task I need to do:

Xml file
##################################################
<REQUEST_GROUP VersionID="2">
<REQUESTING_PARTY>
<PREFERRED_RESPONSE _Format="TEXT" />
</REQUESTING_PARTY>
<REQUEST Account="11111" Password="aaaaaa">
<REQUEST_DATA>
<THE_REQUEST VersionID="2">
<INFO>
<PERSON _FirstName="XXXXX" _LastName="YYYYY" _SSN="123456789">
<_LOCATION _City="ZZZZZZ" _State="CT" _PostalCode="12345">
<STREET_ADDRESS _HouseNumber="123" _StreetName="SSSS DDDD" />
</_LOCATION>
</PERSON>
</INFO>
</THE_REQUEST>
</REQUEST_DATA>
</REQUEST>
</REQUEST_GROUP>
##################################################

What I need to do is to change some data like HouseNumber="123" or _StreetName="SSSS DDDD" to something else and save into the same XML file.

I am a bit lost how to do it. All of the examples I get tell me how to read the data but not how to change it and write it out.

Please assist if you know the answer. Any hint should be good enough.

Thank you.
 
Have a look at
Worst case scenario, XML files are basically tagged text streams (files), so Perl can do anything you want with them, including reading them in, and putting nothing out ...

read the streams, and use substr, regexes, whatever you need to do the job based on what you know, you can always improve on first attempts later, have a go ...

Any problems call back

--Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top