XML file - cannot extract nodes

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-
andy7789Asked:
Who is Participating?
 
f_o_o_k_yConnect With a Mentor Commented:
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
 
f_o_o_k_yCommented:
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
 
andy7789Author Commented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
f_o_o_k_yCommented:
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
 
andy7789Author Commented:
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
 
andy7789Author Commented:
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
 
f_o_o_k_yCommented:
try
echo node_content($obj);  or $obj[0]
As I wrote before i have only php 5 and there is DOMDocument
0
 
andy7789Author Commented:
Call to undefined function: node_content()

$obj[0] - returns nothing
0
 
andy7789Author Commented:
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
 
f_o_o_k_yCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.