HeadScratcher has asked for the wisdom of the Perl Monks concerning the following question:
I'm a humble peasant seeking the wisdom of the holy ones please enlighten me
I'm attempting to extract elements from an XML file
in the code bellow how do I adapt it so I can tell the difference between the 2 'title' fields and how do I get the value of point?
I'm hoping if I can work this out I can use the same principle to extract other items like author
#!C:\strawberry\perl\bin\perl.exe use strict; use warnings; use XML::LibXML; use XML::LibXML::XPathContext; use Data::Dump qw(dump); my $filename = 'georss.xml'; my $dom = XML::LibXML->load_xml(location => $filename); my $xpc = XML::LibXML::XPathContext->new($dom); $xpc->registerNs(dft => "http://www.w3.org/2005/Atom"); $xpc->registerNs(georss => "http://www.georss.org/georss"); my $title = $xpc->findnodes('//dft:title'); print "title $title\n"; my $point = $xpc->findnodes('//georss:where/point'); print "point $point\n";
OUTPUT from above code
title EarthquakesM 3.2, Mona Passage
point
georss.xml
<?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.geo +rss.org/georss" xmlns:gml="http://www.opengis.net/gml"> <title>Earthquakes</title> <subtitle>International earthquake observation labs</subtitle> <link href="http://example.org/" /> <updated>2005-12-13T18:30:02Z</updated> <author> <name>Dr. Thaddeus Remor</name> <email>tremor@quakelab.edu</email> </author> <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id> <entry> <title>M 3.2, Mona Passage</title> <link href="http://example.org/2005/09/09/atom01" /> <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id> <updated>2005-08-17T07:02:32Z</updated> <summary>We just had a big one.</summary> <georss:where> <point>45.256 -71.92</point> </georss:where> </entry> </feed>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: perl XML::LibXML help reading an xml file that uses namespaces (updated)
by haukex (Archbishop) on Aug 07, 2016 at 14:16 UTC | |
by HeadScratcher (Novice) on Aug 07, 2016 at 14:50 UTC |