2

I decided to use eXist as a database for an application that I am writing in Perl and I am experimenting with it. The problem is that I have stored a .xml document with the following structure

<foo-bar00>
    <perfdata datum="GigabitEthernet3_0_18">
        <cli cmd="whatsup" detail="GigabitEthernet3/0/18" find="" given="">
            <input_rate>3</input_rate>
            <output_rate>3</output_rate>
        </cli>
    </perfdata>
    <timeline>2011-5-23T11:15:33</timeline>
</foo-bar00>

and it is located in the "/db/LAB/foo-bar00/2011/5/23/11_15_33.xml" collection.

I can successfully query it, like

my $xquery = 'doc("/db/LAB/foo-bar00/2011/5/23/11_15_33.xml")' ;

or $xquery can be equal to = doc("/db/LAB/foo-bar00/2011/5/23/11_15_33.xml")/foo-bar00/perfdata/cli/data(output_rate) or = doc("/db/LAB/foo-bar00/2011/5/23/11_15_33.xml")/foo-bar00/data(timeline)

my ($rc1, $set) = $eXist->executeQuery($xquery) ;
my ($rc2, $count) = $eXist->numberOfResults($set) ;
my ($rc3, @data) = $eXist->retrieveResults($set) ;
$eXist->releaseResultSet($set) ;
print Dumper(@data) ;

And the result is :

$VAR1 = {
  'hitCount' => 1,
  'foo-bar00' => {
    'perfdata' => {
      'cli' => {
        'given' => '',
        'detail' => 'GigabitEthernet3/0/18',
        'input_rate' => '3',
        'cmd' => 'whatsup',
        'output_rate' => '3',
        'find' => ''
      },
      'datum' => 'GigabitEthernet3_0_18'
    },
    'timeline' => '2011-5-23T11:15:33'
  }
};

---> Given that I know the xml document that I want to retrieve info from. ---> Given that I want to retrieve the timeline information.

When I am writing :

my $db_xml_doc = "/db/LAB/foo-bar00/2011/5/23/11_15_33.xml" ;
my ($db_rc, $db_datum) = $eXist->queryXPath("/foo-bar00/timeline", $db_xml_doc, "") ;
print Dumper($db_datum) ;

The result is :

$VAR1 = {
  'hash' => 1717362942,
  'id' => 3,
  'results' => [
    {
      'node_id' => '1.2',
      'document' => '/db/LAB/foo-bar00/2011/5/23/11_15_33.xml'
    }
  ]
};

The question is : How can I retrieve the "timeline" info ? Seems that the "node_id" variable (=1.2) can points to the "timeline" info, but how can I use it ?

Thank you.

Joe Wicentowski
  • 5,159
  • 16
  • 26
user690182
  • 269
  • 1
  • 8
  • 20

2 Answers2

0

Don't know if you're still interested, but you could either retrieve the doc as DOM and apply an xquery to the DOM, or, probably better, only pull out the info you want in the query that you submit to the server.

Something like this:

for $p in doc("/db/LAB/foo-bar00/2011/5/23/11_15_33.xml")//output_rate
return
    <vlaue>$p</value>
ЯegDwight
  • 24,821
  • 10
  • 45
  • 52
jpe
  • 1
  • 2
0
use XML::LibXML qw( );

my $parser = XML::LibXML->new();
my $doc    = $parser->parse_file('a.xml');
my $root   = $doc->documentElement();

my ($timeline) = $root->findnodes('timeline');
if ($timeline) {
    print("Exists: ", $timeline->textContent(), "\n");
}

or

my ($timeline) = $root->findnodes('timeline/text()');
if ($timeline) {
    print("Exists: ", $timeline->getValue(), "\n");
}

I could have used /foo-bar00/timeline instead of timeline, but I didn't see the need.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • thank you ikegami, but i am not asking how to parse a .xml file, but how to to retrieve a node that is stored inside the database (there is no file, in other words, and downloading it from the database in order to parse it will not be efficient) – user690182 May 25 '11 at 10:58
  • @user690182, I didn't realise `eXist` was a library. There's no links to documentation or anything. – ikegami May 25 '11 at 19:27