Solved

XML file - cannot extract nodes

Posted on 2007-04-05
12
304 Views
Last Modified: 2013-11-18
Hi X-perts,

here is the code:
<?

      $xmlstg = file_get_contents('http://api.hostip.info/?ip=12.215.42.19');
      $dom = domxml_open_mem($xmlstg);

       $doc = new DomDocument($xmlstg); //the second variant
        $calcX = &$dom->xpath_new_context();
      
      //$obj = $calcX->xpath_eval("//HostipLookupResultSet/description/text()");
      //$obj = $calcX->xpath_eval("//HostipLookupResultSet/description/");
      $obj = $calcX->xpath_eval("//description/text()");
      echo 'value'.$obj[0];

      $dom->free();
?>

It does not return the node value. This is the xml file I am trying to parse:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<HostipLookupResultSet version="1.0.0" xmlns="http://www.hostip.info/api" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.hostip.info/api/hostip-1.0.0.xsd">
 <gml:description>This is the Hostip Lookup Service</gml:description>
 <gml:name>hostip</gml:name>
 <gml:boundedBy>
  <gml:Null>inapplicable</gml:Null>
 </gml:boundedBy>
 <gml:featureMember>

  <Hostip>
   <gml:name>Sugar Grove, IL</gml:name>
   <countryName>UNITED STATES</countryName>
   <countryAbbrev>US</countryAbbrev>
   <!-- Co-ordinates are available as lng,lat -->
   <ipLocation>
    <gml:PointProperty>

     <gml:Point srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">
      <gml:coordinates>-88.4588,41.7696</gml:coordinates>
     </gml:Point>
    </gml:PointProperty>
   </ipLocation>
  </Hostip>
 </gml:featureMember>
</HostipLookupResultSet>
=====================================

What is wrong in my code? Please, help.

Thanks

-A-
0
Comment
Question by:andy7789
  • 5
  • 5
12 Comments
 
LVL 11

Expert Comment

by:f_o_o_k_y
ID: 18862929
This should help:
But i couldn't test it
<?

      $xmlstg = file_get_contents('http://api.hostip.info/?ip=12.215.42.19');
      $dom = domxml_open_mem($xmlstg);

       $doc = new DomDocument($xmlstg); //the second variant
        $calcX = &$dom->xpath_new_context();
     
      //$obj = $calcX->xpath_eval("//HostipLookupResultSet/description/text()");
      //$obj = $calcX->xpath_eval("//HostipLookupResultSet/description/");
      $obj = $calcX->xpath_eval("//gml:description");
      echo 'value'.$obj[0];

      $dom->free();
?>



but in PHP 5 better use: (tested and working)
<?php
$doc = new DOMDocument();

$doc->loadXML(file_get_contents('http://api.hostip.info/?ip=12.215.42.19'));

$xpath = new DOMXPath($doc);

$query ='//gml:description';

$items = $xpath->query($query);



foreach ($items as $item) {

    echo $item->nodeValue;

}
?>

Best Regards
FooKy
0
 

Author Comment

by:andy7789
ID: 18863021
FooKy,

Thank you. The 1st code generates the errors:

Warning: xpath_eval(): Undefined namespace prefix in ....test4.php on line 11
Warning: xpath_eval(): xmlXPathEval: evaluation failed in ....test4.php on line 11

It is something with this prefix 'gml:description'
======================

The 2nd code also returns errors:

Warning: domdocument() expects at least 1 parameter, 0 given in .....test4.php on line 2
Fatal error: Call to undefined function: loadxml() in .....test4.php on line 4

Any more ideas how to fix this? I think the easiest way would be to fix the name prefix issue.

Thanks

A
0
 
LVL 11

Expert Comment

by:f_o_o_k_y
ID: 18863042
try (because i cannot test this)
<?

      $xmlstg = file_get_contents('http://api.hostip.info/?ip=12.215.42.19');
      $dom = domxml_open_mem($xmlstg);

       $doc = new DomDocument($xmlstg); //the second variant
        $calcX = &$dom->xpath_new_context();
     
      $obj = $calcX->xpath_eval("//HostipLookupResultSet/gml:description");
// or  
//    $obj = $calcX->xpath_eval("//HostipLookupResultSet/gml:description/text()");
 
      echo 'value'.$obj[0];

      $dom->free();
?>


Which version of PHP do you use?
0
 

Author Comment

by:andy7789
ID: 18863173
it is php 4.3.4

It generates the same errors as in my previous post:

Warning: xpath_eval(): Undefined namespace prefix in ....test4.php on line 11
Warning: xpath_eval(): xmlXPathEval: evaluation failed in ....test4.php on line 11

It works OK, if I parse a simple xml file without namespace prefix - for example like

$xml='<MY_SERVICE>
    <MERCHANDISE>
        <SERVICE TYPE="books">
            <NAME>Ulysses</NAME>
        </SERVICE>
        <SERVICE TYPE="books">
            <NAME>The Poisonwood Bible</NAME>
        </SERVICE>
    </MERCHANDISE>
</MY_SERVICE>';

So, the problem is with that prefix
0
 

Author Comment

by:andy7789
ID: 18863276
i have found the way how to manage that prefix:

<?

    $xmlstg = file_get_contents('http://api.hostip.info/?ip=12.215.42.19');
    $dom = domxml_open_mem($xmlstg);

    $doc = new DomDocument($xmlstg); //the second variant
    $calcX = &$dom->xpath_new_context();
      $calcX->xpath_register_ns("gml","http://www.opengis.net/gml");
 
      $obj = $calcX->xpath_eval("//gml:description/text()");//the 1st variant
      $nodes = $dom->get_elements_by_tagname("gml:description",$calcX);//the 2nd variant
 
      echo 'value'.$obj[0];
        echo 'nodes'.$nodes[0];

      $dom->free();
?>

It does not returns any errors, but still the values are empty
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Expert Comment

by:f_o_o_k_y
ID: 18864970
try
echo node_content($obj);  or $obj[0]
As I wrote before i have only php 5 and there is DOMDocument
0
 

Author Comment

by:andy7789
ID: 18867258
Call to undefined function: node_content()

$obj[0] - returns nothing
0
 
LVL 11

Accepted Solution

by:
f_o_o_k_y earned 500 total points
ID: 18871854
Try testing this code and give me again output. Sorry for not testing this but I have only PHP  5 as I said befor and until wednesday I can't get php 4 with PECL.

<?
    $xmlstg = file_get_contents('http://api.hostip.info/?ip=12.215.42.19');
    $dom = domxml_open_mem($xmlstg);

    $doc = new DomDocument($xmlstg); //the second variant
    $calcX = &$dom->xpath_new_context();
      $calcX->xpath_register_ns("gml","http://www.opengis.net/gml");
 
      $node = $calcX->xpath_eval("//gml:description/text()");//the 1st variant
//please test both

      //$node = $dom->get_elements_by_tagname("gml:description",$calcX);//the 2nd variant


foreach($node->nodeset as $content){
     echo $content;   //maybe this
     echo $content->content; //or this
    }
      $dom->free();
?>



0
 

Author Comment

by:andy7789
ID: 18873859
FooKy,

Thank you. echo $content->content; returns correct values for the tagnames with namespace prefixes, but generates an error (Invalid argument supplied for foreach() ) for normal tagnames.

I have installed an extra xml parser classses DOMIT from http://sourceforge.net/project/showfiles.php?group_id=96364 and it works perfectly on PHP4. A lot of functions and options.... It easily extracts any nodes with or without prefixes
0
 
LVL 11

Expert Comment

by:f_o_o_k_y
ID: 18884344
so use it without foreach. (but I dont know what will happen when there is more than one element with the same tag) so try and test it.
Best Regards
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now