Solved

XML file - cannot extract nodes

Posted on 2007-04-05
12
333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

688 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