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
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.