in reply to parsing & retrieving from an XML file

Here is a way to extract all the necessary info from your XML using XML::LibXML and simple xpath queries. You will probably want to save the results to a data structure (an array of hashes should work) rather than printing them out like I do, but printing them out first allows you to see if it is working correctly.

See XML::LibXML::Node and XML::LibXML::Element for more details.

use XML::LibXML; my $doc = XML::LibXML->load_xml(location => '/path/to/file.xml'); my @envdetails = $doc->findnodes('//ENVDETAILS'); for my $envdetail (@envdetails) { my $id = $envdetail->getAttribute('id'); print "id: $id\n"; my $dmgrhost = $envdetail->findvalue('DMGRHOST'); print "DMGRHOST: $dmgrhost\n"; my @nodes = $envdetail->findnodes('NODE'); for my $node (@nodes) { my $nodeid = $node->getAttribute('nodeid'); print "nodeid: $nodeid\n"; my $nodehost = $node->findvalue('NODEHOST'); print "NODEHOST: $nodehost\n"; my @jvms = $node->findnodes('JVM'); for my $jvm (@jvms) { my $jvmtype = $jvm->getAttribute('jvmtype'); if ($jvmtype eq 'api') { my @jvmnames = $jvm->findnodes('JVMNAME'); for my $jvmname (@jvmnames) { my $name = $jvmname->textContent; print "JVMNAME: $name\n"; } } } } } # OUTPUT id: abc DMGRHOST: a.b.c.d nodeid: 1 NODEHOST: v.x.y.z JVMNAME: abc_api1.1 JVMNAME: abc_api1.2

Replies are listed 'Best First'.
Re^2: parsing & retrieving from an XML file
by sbafna (Novice) on Nov 07, 2017 at 08:12 UTC
    Thanks a lot for giving it a try!! Appreciate it. I will try & post the results. What I understood is that I need to create my own data structure to save & retrieve it.