Click here to get back home

how to check for existenceof XML tags with XML::Simple?

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
how to check for existenceof XML tags with XML::Simple? Ted Johnson 01-29-2005
Get Chitika Premium
Posted by Ted Johnson on January 29, 2005, 12:45 pm
Please log in for more thread options
Using XML::Simple, what's the best way to check for
the existence of a particular XML tag?

For example, lets say I have 2 files, "success.xml" and
"failure.xml" which differ only in their innermost tags
(one being "<responseSuccess>" and the other being "<responseFailed>").

Can I use "exists()" or "defined()" on the path which refers
to that tag?

For example, consider the following:

------ START: success.xml -------

<?xml version="1.0" encoding="UTF-8"?>
<response>
<header>
<responseStatus>
<responseSuccess>
It worked, blah blah
</responseSuccess>
</responseStatus>
</header>
</response>

------ END: success.xml -------

------ START: failure.xml -------
<?xml version="1.0" encoding="UTF-8"?>
<response>
<header>
<responseStatus>
<responseFailed>
It failed, blah blah blah
</responseFailed>
</responseStatus>
</header>
</response>
------ END: failure.xml -------


Is this the correct way to check for tag existence? :

#!/opt/perl/bin/perl
use XML::Simple;
use Data::Dumper;

$xml = new XML::Simple;
$data = $xml->XMLin("./$ARGV[0]");

if (defined( $data->
 HomeNewsGroups ||
->->)) {
# then it worked
}
elif (defined($data->
 HomeNewsGroups ||
->->)) {
# then it failed
}



or is there a better way?
Thanks in advance.
-Ted

PS. I have no control over the format of the XML files - they come
from another app.



Posted by John Bokma on January 29, 2005, 10:51 pm
Please log in for more thread options
Ted Johnson wrote:

> Using XML::Simple,

Why?

> what's the best way to check for
> the existence of a particular XML tag?

XML::Parser and defining a start handler is one way.

See http://johnbokma.com/perl/finding-unique-xml-elements.html
for a small example.

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html



Similar ThreadsPosted
Re: match nested tags May 3, 2006, 5:20 pm
XML::LibXSLT element tags stripped out of transformation March 21, 2005, 6:32 pm
SVN::Agent check out? August 10, 2008, 4:52 pm
check response with mod_perl October 22, 2004, 11:23 am
Net::SSH::Perl - Corrupted check bytes February 8, 2006, 9:33 am
perl xml check for element closure November 18, 2006, 9:26 pm
Email address syntax check? December 2, 2006, 2:58 pm
RFC + sanity check, Tk::Text with Vi interface April 18, 2008, 7:29 pm
How does signature check in CPAN work? September 25, 2008, 7:02 am
Is there a module that can check if user "foo" may read a file? August 5, 2005, 3:59 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap