Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: parsing & retrieving from an XML file

by tangent (Vicar)
on Nov 06, 2017 at 20:57 UTC ( #1202883=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1202883]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2022-05-22 10:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (80 votes). Check out past polls.

    Notices?