Results 1 to 1 of 1
Related
-
Help parsing multiple XML files in PHP 5 Forum: PHP Forum
Replies: 5 -
parsing string Forum: CGI Perl Forum
Replies: 1 -
Help parsing html for URLs Forum: CGI Perl Forum
Replies: 0 -
Parsing javascript variable to PHP Forum: PHP Forum
Replies: 0 -
Parsing javascript variable to PHP Forum: Javascript Forum
Replies: 0
-
05-23-2006, 03:44 AM #1
Parsing XML file using XML::LIbXML
Hi everybody...
currently i'm trying to parse an xml file using perl but i got some error. i need to differentiate ELEMENT_NODE and TEXT_NODE, so that my program will only focus on the element_node. Here is the xml file;
<?xml version="1.0"?>
<buoy location="Rondo" position_east="10.82" position_north="34.67">
<data time="2006-03-30-T23:59:59+00:00" location="Rondo" parameter="Wind Speed" height="3.0" unit="m/s">1.234</data>
<data time="2006-03-30-T23:59:59+00:00" location="Rondo" parameter="Wind Speed" height="3.0" unit="m/s">1.234</data>
</buoy>
This is my current code:
use strict;
use XML::LibXML;
use Data:umper;
use constant TEXT_NODE => 3;
use constant ELEMENT_NODE => 1;
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file($filename);
my $root = $doc->getDocumentElement();
print "\nRoot element :", $root->nodeName, "\n";
my ($location, $timestamp, $parameter, $value);
while ($root->nodeName eq "buoy") {
$location = $root->getAttribute('location');
foreach my $child ($root->getChildNodes()) {
while ($child->nodeType == ELEMENT_NODE) {
if ( $child->nodeType == TEXT_NODE) {
next;
}
$value = $child->getFirstChild()->getData();
if ($child->nodeName eq "data") {
$timestamp = $child->getAttribute('time');
$parameter = $child->getAttribute('parameter');
# print $timestamp, "\n", $parameter , "\n";
} # end if
elsif ($child->nodeName ne "data") {
print "Error getting data from This file\n";
} # end elsif
$data{$location}->{$timestamp}->{$parameter} = $value;
} # end while
} # end foreach
} #end while
} # end process
print Dumper (\%data);
while compiling, i only get this:
[hayat@pc-hayat ~]$ perl checking_directory.pl
Root element :buoy
actually, i dont want to make it as hardcode..i prefer softcode..
how to parse this file correctly? Since i'm quite new in perl...could somebody please help me here ...
regards;
alnurhayat
Cloudjiffy- PaaS for Developers
10-05-2020, 12:30 AM in Web Hosting Forum