Sponsored Content
Top Forums UNIX for Advanced & Expert Users Perl XML::DOM: How to test if element exists? Post 302572077 by Juha on Wednesday 9th of November 2011 06:43:24 AM
Old 11-09-2011
Perl XML::DOM: How to test if element exists?

Hi,

I'm trying to write a script for some xml file handling, but I'm not getting too far with it.

I've got the following xml content
Code:
<?xml version="1.0" encoding="UTF-8"?>
<Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
	<Operation name="OPER1">
		<Action name="ACTION1">
			<Condition name="None">
				<Parameter name="AAA">
				</Parameter>
			</Condition>
		</Action>
		<Action name="ACTION2">
			<Action-category name="Normal">
				<Condition name="All">
					<Parameter name="AAA">
	        			</Parameter>
				</Condition>
			</Action-category>
		</Action>
	</Operation>
</Test>

This is just a small piece of the actual file that can be thousands of lines.
Some "Actions" can have "Action-category" but some do not, and I would like to be able to do different processing based on that. I am however unable to test if this element exists.

My code is as follows:

Code:
#!/usr/bin/perl

use XML::DOM;
use strict;
my @files = @ARGV;
my $operation = "OPER1";
my $parser = XML::DOM::Parser->new();
my $doc = $parser->parsefile(@files[0]);

foreach my $oper ($doc->getElementsByTagName('Operation'))
{
   print "OPERATION: " . $oper->getAttribute('name') . "\n";
   if ( $operation eq $oper->getAttribute('name'))
   {
      foreach my $action ($oper->getElementsByTagName('Action'))
      {
         print "ACTION: " . $action->getAttribute('name') . "\n";
         if (defined ($action->getElementsByTagName('Action-category')))
         {
            print "Action-category defined\n";
         }
         else
         {
            print "Action-category NOT defined\n";
         }
      }
   }
   else
   {
      #do something else
   }
}

and it results in to the following output:

Code:
>./parser.pl file.xml
OPERATION: OPER1
ACTION: ACTION1
Action-category defined
ACTION: ACTION2
Action-category defined

So, it says the "action-category" is defined, even though it is not? Smilie

Would anyone have any idea why or how to fix this?
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to parse a XML file using PERL and XML::DOm

I need to know the way. I have got parsing down some nodes. But I was unable to get the child node perfectly. If you have code please send it. It will be very useful for me. (0 Replies)
Discussion started by: girigopal
0 Replies

2. Shell Programming and Scripting

Perl: How to convert xxx@dom.ain.com to domaincom

Hi, Is there a way to convert xxx@dom.ain.com to domaincom, so to remove everything before and including @ and then to remove the dot from the rest of the line? I know I can do: $str = "xxx\@dom.ain.com"; print "String before: $str\n"; $str =~ s/.*?@//; $str =~ s/\.//g; print... (6 Replies)
Discussion started by: Juha
6 Replies

3. Shell Programming and Scripting

XML root element

Hi All Can someone please help me with this awk to search an element in a XML file with a particular value and then change the root element. Thanks & Regards Karan (9 Replies)
Discussion started by: karansachdeva
9 Replies

4. Shell Programming and Scripting

Extract XML Element Values

I have a rather large file with XML-style content. Each line contains one full XML entry. For example: 1:<Message><DNIS>1234</DNIS><UCID>3456</UCID><TransferGroup>XYZXYZ</TransferGroup></Message> 2:<Message><DNIS>9999</DNIS><UCID>2584</UCID><TransferGroup>ABCABC</TransferGroup></Message>... (1 Reply)
Discussion started by: sharpi03
1 Replies

5. UNIX Desktop Questions & Answers

read XML xml element with REGEXP

Hi, I would need to read an xml element from an xml file to a local variable. Please could you help me with a shell script to get so? Considering that I have a file called file.xml like below: <header> <description>This is the description</description> <content>This is the... (2 Replies)
Discussion started by: oscarmon
2 Replies

6. Programming

DOM query selected path and show as xml

First, I am sorry if this question not this room scope, I have a XML file : file: book.xml <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> ... (3 Replies)
Discussion started by: penyu
3 Replies

7. Shell Programming and Scripting

Test if string exists

How do I use bash to test whether a string occurs more than two times in a file? (2 Replies)
Discussion started by: locoroco
2 Replies

8. AIX

Perl Module (XML:DOM) dependency

Hi All, I am facing dependency on AIX :confused:.I am trying to run an script which requires PERL MODULE (XML::DOM) to be installed. Please find the attached file which shows the error i am getting (cant locate XML/DOM.pm in @INC). Please let me know how to install PERL MODULE (XML::DOM) ... (3 Replies)
Discussion started by: srivatsa86
3 Replies

9. Shell Programming and Scripting

Find if XML element has a matching required element

I want to check if every <Part> element has corresponding <Description> in this sample XML. ....<Lot Of XML> <Inv lineNumber="2"> <Item> ... (4 Replies)
Discussion started by: kchinnam
4 Replies

10. Shell Programming and Scripting

If test array element multiplication

Ya, I know, who in this day and age is mirroring rootvg...? But yes, my shop does and I need to script checking for it. I also know I could just inverse the the logic and call the LV mirrored if the LPs and PPs were not equal. But I want to do the math in the if test and also know I could... (5 Replies)
Discussion started by: gtsonoma
5 Replies
XML::DOM::Parser(3)					User Contributed Perl Documentation				       XML::DOM::Parser(3)

NAME
XML::DOM::Parser - An XML::Parser that builds XML::DOM document structures SYNOPSIS
use XML::DOM; my $parser = new XML::DOM::Parser; my $doc = $parser->parsefile ("file.xml"); $doc->dispose; # Avoid memory leaks - cleanup circular references DESCRIPTION
XML::DOM::Parser extends XML::Parser The XML::Parser module was written by Clark Cooper and is built on top of XML::Parser::Expat, which is a lower level interface to James Clark's expat library. XML::DOM::Parser parses XML strings or files and builds a data structure that conforms to the API of the Document Object Model as described at <http://www.w3.org/TR/REC-DOM-Level-1>. See the XML::Parser manpage for other additional properties of the XML::DOM::Parser class. Note that the 'Style' property should not be used (it is set internally.) The XML::Parser NoExpand option is more or less supported, in that it will generate EntityReference objects whenever an entity reference is encountered in character data. I'm not sure how useful this is. Any comments are welcome. As described in the synopsis, when you create an XML::DOM::Parser object, the parse and parsefile methods create an XML::DOM::Document object from the specified input. This Document object can then be examined, modified and written back out to a file or converted to a string. When using XML::DOM with XML::Parser version 2.19 and up, setting the XML::DOM::Parser option KeepCDATA to 1 will store CDATASections in CDATASection nodes, instead of converting them to Text nodes. Subsequent CDATASection nodes will be merged into one. Let me know if this is a problem. Using LWP to parse URLs The parsefile() method now also supports URLs, e.g. http://www.erols.com/enno/xsa.xml. It uses LWP to download the file and then calls parse() on the resulting string. By default it will use a LWP::UserAgent that is created as follows: use LWP::UserAgent; $LWP_USER_AGENT = LWP::UserAgent->new; $LWP_USER_AGENT->env_proxy; Note that env_proxy reads proxy settings from environment variables, which is what I need to do to get thru our firewall. If you want to use a different LWP::UserAgent, you can either set it globally with: XML::DOM::Parser::set_LWP_UserAgent ($my_agent); or, you can specify it for a specific XML::DOM::Parser by passing it to the constructor: my $parser = new XML::DOM::Parser (LWP_UserAgent => $my_agent); Currently, LWP is used when the filename (passed to parsefile) starts with one of the following URL schemes: http, https, ftp, wais, gopher, or file (followed by a colon.) If I missed one, please let me know. The LWP modules are part of libwww-perl which is available at CPAN. perl v5.16.3 2002-07-31 XML::DOM::Parser(3)
All times are GMT -4. The time now is 06:49 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy