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

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"?>
         It worked, blah blah

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

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

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

use XML::Simple;
use Data::Dumper;

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

if (defined( $data->{header}->{responseStatus}->{responseSuccess})) {
   # then it worked
elif (defined($data->{header}->{responseStatus}->{responseFailed})) {
   # then it failed

or is there a better way?
Thanks in advance.

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

ted53 (4)
1/29/2005 11:45:23 AM
comp.lang.perl.modules 4194 articles. 0 followers. jerrykrinock (6) is leader. Post Follow

1 Replies

Similar Articles

[PageSpeed] 32

Ted Johnson wrote:

> Using XML::Simple,


> 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
postmaster17 (329)
1/29/2005 9:51:31 PM

Similar Artilces:

Perl XML::Simple Accessing complex XML
Hi <companyname count="1"> <property id="55467" md="2008-03-20" mc="GBP" mp="173000" > <psumm><![CDATA[This 3 bedroom, ]]></psumm> <a1>1 Bucket Way</a1> <at>Stafford</at> <ac>Staffordshire</ac> <actry></actry> <images> <image id="543">Property Image</image> <image id="545">Property Image</image> </images> </property> </companyname> I am trying to read the above (simplified) with XML::Simple...

Off Topic - Databindings and XML
Dear Group First of all, my apologies for posting here. I tried the MS groups but they're so useless. As some of you work with ASP.NET and XML I wondered if you have come across this and can give me some advice as this group is the only reliable source for an answer. Again, my apologies for posting it here. I wonder if I do something wrong and whether this is possible at all. Any hint is greatly appreciated. I have a XML file bound to a Treeview. The treenodes Text/Value properties bind and populate fine if I set them to an attribute of the XML tag. What I would like to do however is s...

Different results parsing a XML file with XML::Simple (XML::Sax vs. XML::Parser)
Hello Usenet. I'm subject to some confusion with XML and UTF8. I'm working with XML-Simple and I try to decode some XML with with german umlauts (ISO-8859-1). The first XML line declared the encoding correct (see code below). But I'm getting different results using XML-Simple with the default XML parser named XML::Sax and a second parser named XML::Parser. The following code tries to decode the mini XML file and prints the UTF8 flags of the resulting strings. Can someone run this code on his machine and post the results? Thanks. The results on my machine are this: ���ä...

To XML or not to XML?
Ok, it has been a while since I've needed to make an involved web page. Coming back on the scene now is a bit confusing, what with all the cool stuff to use like XML, PHP, MySQL, etc... So here is my question. I'm in search of a good dynamic way to generate web pages which separates content from presentation. I am seeking to create a web site that, to put it succinctly, has a list of users and sets of information corresponding to each user. I am familiar with how to do this using PHP, MySQL, and regular old HTML. I maintain the user database in MySQL and dynamically present it and th...

I have a gnucash file that I'm trying to transform into an identical XML file without the invoices. I've been at this for a while now. The gnucash file is in XML and the element I'm looking into is: <gnc:transaction> I want to ignore those that contain <slot:key>gncInvoice</slot:key> which is a child of slot which is a child of slots which is a child of transactions. Any help you're willing to provide would be greatly appreciated. Thanks. Classic application for XSLT. Start with the identity transformation, then add a template which matches the ones yo...

XML or not XML?
I am developing a simple MySQL database editor, and am looking into creating an PHP/AJAX app as the GUI. A lot of this in new ground, and much more is dusting off things I haven't used in a while. My question though involves whether or nor to employ XML. I did some XML a few years back, but don't typically bother anymore. As I see it XML is great when I need to have an app with a standard data presentation that other developers can easily interface. I.E. like a bank statement that may feed a variety of applications developed by a variety of clients. However, if I am develo...

Installation of: XML-DOM-1.44, XML-Parser-2.36, XML-RSS-1.47, XML-Simple-2.18
- CPAN Forum Q&A, Installation of; -- XML-Writer-0.606 -- XML-DOM-1.44 -- XML-Simple-2.18 -- XML-RSS-1.47 -- XML-Parser-2.36 As concerns the installation of the four items above, these errors are continously flagged as an intervention to the process; XML-Simple-2.18, flagged: ======================== Checking installed modules... could not find ParseDetails.ini in C: \strawberry\perl\vendor\lib\xml\sax XML::Sax is installed, it will be used by the test suite Could not open 'lib/XML/Simple.pm': No such file or directory at C:/ strawberry/perl/lib/ExtUtils/MM_Unix.pm line 2627. --...

embedding xml in xml as non-xml :)
Hi all, I have an application that logs in xml. Assume <xmlLog></xmlLog>. In this element the app logs anything it gets from foreign hosts. Now if the host sends xml data, the structure of the document changes. ie. <xmlLog><somTag></somTag></xmlLog>. This will cause problems with my log reader, because it assumes that <xmlLog/> contains non-xml data. My question is, is there a way to treat the data in the <xmlLog/> element as non xml data. Something I can do that would treat anything this element contains as a literal? Any help or ...

Printing XML string With XML tags
Hello all, I;m a beginner with XML. All I want is to print the XML string with tags. In the following example, there is function called: xmlNodeGetString. This function is getting a char* back. The result of this function is the XML elements of the list without tags (Jaap, Kees, Kris). at the end of this example a function xmlSaveFormatFile writes a XML file. looking like: <?xml version="1.0"?> <LinkedList><Element>Jaap</Element><Element>Kees</Element><Element>Kris</Element></LinkedList> So what I want to do is writing the...

Serialize XML without ?xml tag
Hello: I am using LSSerializer to serialize an XML document to a string. It generates this tag at the top of this string: <?xml version="1.0" encoding="UTF-16"?> I need to generate the XML without that. How can I do that? Thanks, Neil -- Neil Aggarwal, (281)846-8957, www.JAMMConsulting.com Will your e-commerce site go offline if you have a DB server failure, fiber cut, flood, fire, or other disaster? If so, ask about our geographically redundant database system. Neil wrote: > I am using LSSerializer to serialize an XML document to a string. > It generate...

XML Parsing too slow with XML::Simple
I have some files ranging from 6 meg to 15 meg that I need to proces to input into a database. I'm inheriting some old code usin XML::Simple and this process is taking forever (an hour or more). have eliminated the database as the time hog in this equation is there an alternative XML module I should be using Sent via Archivaty.com "jabby" <krising@gmail-dot-com.no-spam.invalid> wrote in message news:SdadnTQ6M66PteLeRVn-hg@giganews.com... > I have some files ranging from 6 meg to 15 meg that I need to process > to input into a database. I'm inheriting some old ...

no tags in my xml-to-xml transformation, help....
my xml-to-xml transformation is outputting an xml header and then just text like this: <?xml version="1.0" encoding="iso-8859-1"?>e19dbb7b-5529-43a0...... I've searched regarding this problem and it seems that if there's no match in your xsl template then by default everything gets spat out. But I've tested this with very definite matches in my template, and still there are no xml tags in the output. Has anyone seen this before? Any help much appreciated.... Yannick yannicklaclau wrote: > my xml-to-xml transformation is outputting an xml heade...

XML::Simple in perl?
Using XML::Simple in perl is extreemly slow to parse big XML files (can be up to 250M, taking ~1h). How can I increase my performance / reduce my memory usage? Is SAX the way forward? Should I consider using (learning) Expat.c for increased performance? How long would parsing a 250M XML file take with Expat? Thanks for any suggestions you can give, Dan. yDan wrote at Thu, 31 Jul 2003 12:41:24 +0100: > Using XML::Simple in perl is > extreemly slow to parse big > XML files (can be up to 250M, > taking ~1h). XML::Simple is not the only module on CPAN. There are also XML::Smar...

Bad XML Causes XML::Simple to exit
Hi all, I am using XML::Simple to parse a basic XML document. If, however, the XML document is not well formed, the script terminates with a mismatched tag error. I would prefer to handle the error myself. I have searched on the web, but it seems that XML::Simple only has XMLin and XMLout functions. Is there any way to change this behaviour using XML::Simple? Cheers, Ben Ben Holness <usenet@bens-house.org.uk> wrote: > If, however, the XML document is not well formed, the script terminates > with a mismatched tag error. Yes, as per XML parsing requirements. (At least, that...

Web resources about - how to check for existenceof XML tags with XML::Simple? - comp.lang.perl.modules

Resources last updated: 2/13/2016 3:04:19 PM