how to get the data from the following xml:

<HostipLookupResultSet version="1.0.1" xsi:noNamespaceSchemaLocation="http://www.hostip.info/api/hostip-1.0.1.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>
<ip>182.0.0.1</ip>
<gml:name>Private</gml:name>
<countryName>Private</countryName>
<countryAbbrev>PR</countryAbbrev>
<!-- Co-ordinates are available as lng,lat -->
<ipLocation>
<gml:pointProperty><gml:Point srsName="http://www.abc.net/xyz/srs/epsg.xml#4326">
<gml:coordinates>68.7833,30.4</gml:coordinates>
</gml:Point></gml:pointProperty>
</ipLocation>
</Hostip>
</gml:featureMember>
</HostipLookupResultSet>

following is what i was trying:

$userIpAddr = "test.xml";
$ipLink= new SimpleXMLElement($userIpAddr, NULL, TRUE);
            $user_ip = $ipLink->xpath("/HostipLookupResultSet/Hostip/ip");
            $user_country = $ipLink->xpath("/HostipLookupResultSet/Hostip/countryName");
            foreach($user_ip as $ip)
            {
                foreach($user_country as $country)
                {
                    echo $ip.'<br/>';
                    echo $country.'<br/>';
                }
            }

is this a correct way to access the data?? if isn't then please suggest me the way to access the asked data...

thnx diafol for your reply and for help. couldn't i do this it using xpath?

Member Avatar for diafol

The problem I saw (like you) was the namespace. Maybe if you register the gml:

http://php.net/manual/en/simplexmlelement.registerxpathnamespace.php

But this seems to work:

$userIpAddr = "test.xml";
$sxe= new SimpleXMLElement($userIpAddr, NULL, TRUE);

$result = $sxe->xpath('//ip');          
while(list( , $node) = each($result)) {
    echo $node;
}

$result = $sxe->xpath('//countryName');         
while(list( , $node) = each($result)) {
    echo $node;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.