MarkovChain has asked for the wisdom of the Perl Monks concerning the following question:
Hello Venerable Monks,
I know this topic has been bought up before and I am probably being criminal here by being repetitious. But I have spent a bit of time on my code here and I fail to see my oversight.... I would appreciate if someone could help me out here.
I have an xml file that I am trying to parse via XPath expressions. I had initially thought about using XML::Twig but since my parsing is never going to be huge, I am sticking with XML::LibXML; especially because I need to be validating my input with a local schema prior to the actual parsing.
The xml file is:
<?xml version="1.0" encoding="UTF-8"?> <instruction_request xmlns="http://www.somedomain.tld/market_reg/admin +_server/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.somedomain.tld/market_reg/admin_ser +ver/1.0 admin_server.xsd"> <request> <action_request> <action>START</action> <instance_information> <application_type> SomeApp </application_type> <application_name> app_name </application_name> <environment>QC</environment> <application_host_ip>10.10.24.56</application_host_ip> <application_host_name> somehost.domain_name.tld </app +lication_host_name> </instance_information> </action_request> </request> <request> <action_request> <action>STOP</action> <instance_information> <application_type> SomeApp2 </application_type> <application_name> instance_name </application_name> <environment>QC</environment> <application_host_ip>172.16.24.56</application_host_ip +> <application_host_name> somehost.domain_name.tld </app +lication_host_name> </instance_information> </action_request> </request> <request> <info_request> <action>GET_INFO</action> </info_request> </request> </instruction_request>
Now my parsing code is:
use strict; use warnings; use XML::LibXML; use Data::Dumper; use XML::LibXML::XPathContext; use strict; use warnings; use XML::LibXML; use Data::Dumper; use XML::LibXML::XPathContext; ############################################################ ######## VARIABLE DECLARATION ######## ############################################################ my $file = '/Volumes/UserData/Users/dattanik/Programs/XML/test_xml.xml'; ############################################################ ######## MAIN PROGRAM ######### ############################################################ #---------------------------------------------------------- # Create a new parser. #---------------------------------------------------------- my $parser = XML::LibXML->new (); #---------------------------------------------------------- # Parse the XML document. #---------------------------------------------------------- my $dom = $parser->load_xml (location => $file); #print ref ($dom) , "\n"; # XML::LibXML::Document #---------------------------------------------------------- # Get the document root. #---------------------------------------------------------- my $root = $dom->getDocumentElement (); #print ref($root), "\n"; # XML::LibXML::Element #---------------------------------------------------------- # Set the context (current) node. #---------------------------------------------------------- $xpc->setContextNode($root); print "Context node set as |". $xpc->getContextNode->nodeName() . "|\n +"; #---------------------------------------------------------- # Make sure you can access data. #---------------------------------------------------------- my $action_requests = $xpc->find('//*', $root); print ref ($action_requests) . "\n"; print 'Size of action requests is ' . $action_requests->size() . "\n"; print 'String value of action requests is "' . $action_requests->strin +g_value() . "\"\n"; print 'To literal of action requests is "' . $action_requests->string_ +value() . "\"\n"; print 'To literal of action requests is "' . $action_requests->get_nod +e(4) . "\"\n"; print 'To element name is "' . $action_requests->get_node(4)->nodeName + (). "\"\n"; print 'To node value is "' . $action_requests->get_node(4)->textConten +t() . "\"\n"; #---------------------------------------------------------- # Get all action requests. # =====> THIS IS WHERE IT GETS FUNKY <========= #---------------------------------------------------------- print '-' x 80 . "\n"; print 'Trying to get the requests directly with an XPath expression .. +.' . "\n"; print '-' x 80 . "\n"; my $action_requests = $xpc->find('//request'); print ref($action_requests) . "\n"; print 'Size of action requests is ' . $action_requests->size() . "\n"; print 'String value of action requests is "' . $action_requests->string_value() . "\"\n"; print 'To literal of action requests is "' . $action_requests->string_value() . "\"\n"; print 'To literal of action requests is "' . $action_requests->get_node(4) . "\"\n"; print 'To element name is "' . $action_requests->get_node(4)->nodeName() . "\"\n"; print 'To node value is "' . $action_requests->get_node(4)->textContent() . "\"\n";
The output is:
$perl newxmltest.pl request: action_request: action: instance_information: application_typ +e: application_name: environment: application_host_ip: application_ho +st_name: request: action_request: action: instance_information: appli +cation_type: application_name: environment: application_host_ip: appl +ication_host_name: request: info_request: action: XML::LibXML::Elemen +t XML::LibXML::Element XML::LibXML::Element XML::LibXML::Element XML::LibXML::Element XML::LibXML::Element XML::LibXML::Element XML::LibXML::Element XML::LibXML::Element XML::LibXML::Element XML::LibXML::Element XML::LibXML::Element XML::LibXML::Element XML::LibXML::Element XML::LibXML::Element XML::LibXML::Element XML::LibXML::Element XML::LibXML::Element XML::LibXML::Element XML::LibXML::Element XML::LibXML::Element Context node set as |instruction_request| XML::LibXML::NodeList Size of action requests is 22 String value of action requests is " START SomeApp app_name QC 10.10.24.56 somehost.domain_name.tld STOP SomeApp2 instance_name QC 172.16.24.56 somehost.domain_name.tld GET_INFO " To literal of action requests is " START SomeApp app_name QC 10.10.24.56 somehost.domain_name.tld STOP SomeApp2 instance_name QC 172.16.24.56 somehost.domain_name.tld GET_INFO " To literal of action requests is "XML::LibXML::Element=SCALAR(0x81c770 +)" To element name is "action" To node value is "START" ----------------------------------------------------------- Trying to get the requests directly with an XPath expression ... ----------------------------------------------------------- XML::LibXML::NodeList Size of action requests is 0 String value of action requests is "" To literal of action requests is "" Use of uninitialized value in concatenation (.) or string at newxmltes +t.pl line 127. To literal of action requests is "" Can't call method "nodeName" on an undefined value at newxmltest.pl li +ne 129.
NOTE: THE OUTPUT HAS BEEN EDITED FROM A FORMATTING PERSPECTIVE .
I am unable to use XPath for instance //request in find / findnodescall's. I have seen examples around the net that claim xpath expresssions work and give examples but they have not worked for me. I am using the latest version of XML::LibXML (version 1.70).
Thank You!!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML::LibXML - parsing question!!
by ikegami (Patriarch) on Dec 18, 2009 at 21:28 UTC | |
by MarkovChain (Sexton) on Dec 19, 2009 at 05:37 UTC | |
by ikegami (Patriarch) on Dec 19, 2009 at 07:30 UTC | |
by MarkovChain (Sexton) on Dec 19, 2009 at 15:40 UTC | |
by ikegami (Patriarch) on Dec 19, 2009 at 17:34 UTC | |
by MarkovChain (Sexton) on Dec 19, 2009 at 16:06 UTC | |
by ikegami (Patriarch) on Dec 19, 2009 at 17:43 UTC | |
by MarkovChain (Sexton) on Dec 19, 2009 at 18:45 UTC | |
| |
|
Re: XML::LibXML - parsing question!!
by Jenda (Abbot) on Dec 21, 2009 at 11:38 UTC | |
by MarkovChain (Sexton) on Dec 22, 2009 at 16:42 UTC |