I have the below complex yet small XML snippet saved in a file, with multiple entries of ENVDETAILS.

<OUTMOST> <MYENVDETAILS> <ENVDETAILS id="abc" > <DMGRHOST>a.b.c.d</DMGRHOST> <CELL>XYZ</CELL> <NODE nodeid="1" > <NODEHOST>v.x.y.z</NODEHOST> <IHSNODE>v_ihsnode</IHSNODE> <IHSJVM ihsjvmtype="log_ihs" > <IHSJVMNAME>abc_logihs1</IHSJV +MNAME> </IHSJVM> <IHSJVM ihsjvmtype="appihs" > <IHSJVMNAME>abc_appihs1</IHSJV +MNAME> </IHSJVM> <JVM jvmtype="nodeagent" > <JVMNAME>nodeagent</JVMNAME> </JVM> <JVM jvmtype="api" > <JVMNAME>abc_api1.1</JVMNAME> <JVMNAME>abc_api1.2</JVMNAME> </JVM> </NODE> </ENVDETAILS> </MYENVDETAILS> </OUTMOST>

Few things we must know:

a). nodeid is not unique.

b). ENVDETAILS id is unique.

I wanted to achieve/perform the following things:

1). Display all the ENVDETAILS ids: [output should have all ids: abc]

2). Display all the jvms corresponding to ENVDETAILS ids & after checking the jvm type: [input: (id=abc)(jvmtype=api) output should have all jvms of type 'api' from ENV id 'abc' i.e.: abc_api1.1, abc_api1.2 ]

3). Display the NODEHOST of JVMNAME is entered as input: [input: jvm=abc_logihs1output should be NODEHOST of jvm 'abc_logihs1' i.e.: v.x.y.z]

4). Display all the jvmtype present on the NODEHOST entered as a input: [input: NODEHOST=v.x.y.z output should be: log_ihs, appihs, nodeagent, api]

5). Display the CELL of NODEHOST is entered as a input: [input: NODEHOST=v.x.y.z output should be: XYZ ]

6). Display the nodeid when NODEHOST is entered as a input: [input: NODEHOST=v.x.y.z output should be: 1 ]

7). Display the DMGR when JVMNAME is entered as input: [input: jvm=abc_api1.1output should be : a.b.c.d ]

8). Display the ENVDETAILS when JVMNAME is entered as input: [input: jvm=abc_api1.1output should be : abc ]

Please help. I am using XML::Smart to achieve few things, however I am learning perl.

I have only written code for 1st point.

use XML::Smart; use strict; use warnings; $ENVFILE="/appl/TOPOLOGY.xml"; my $ENVxml = XML::Smart->new($ENVFILE) || die ("Could not find details + file."); my $env = $ARGV[0]; if ( $ARGV[0] eq "fetch" ) { my @envname=$ENVxml->{OUTMOST}->{MYENVDETAILS}->{ENVDETAILS}(' +[@]','id'); foreach my $i (@envname) { print "$i\n"; } }

In reply to parsing & retrieving from an XML file by sbafna

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.