SimpleXML and weird XML format (namespace?)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paulriedel
Forum Newbie
Posts: 3
Joined: Tue Feb 17, 2009 12:43 pm

SimpleXML and weird XML format (namespace?)

Post by paulriedel »

Hello,

So far I've only worked with easy XML structures and SimeXMLElements.
Now I'm trying to access values from the XML returned by this API: http://api.hostip.info/?ip=12.215.42.19

The XML response looks like this:

Code: Select all

 
<?xml version="1.0" encoding="ISO-8859-1" ?>
<HostipLookupResultSet version="1.0.0" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="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>
 
 
How can I access the values in this XML tree? (preferably using SimpleXML)

Thank you!!

Paul
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: SimpleXML and weird XML format (namespace?)

Post by requinix »

Everytime you want to access a <x:y> node you need to find something that looks like a

Code: Select all

xmlns:x="http://www.example.com/path"
(probably in the root node) and give that URI to, for example, children() as the namespace.

Example:

Code: Select all

$xml = simplexml_load_file("http://api.hostip.info/?ip=12.215.42.19");
print_r($xml->children()); // nothing, no child nodes in the default namespace
print_r($xml->children("http://www.opengis.net/gml"));
paulriedel
Forum Newbie
Posts: 3
Joined: Tue Feb 17, 2009 12:43 pm

Re: SimpleXML and weird XML format (namespace?)

Post by paulriedel »

thank you very much!! it worked!
Post Reply