Extracting from XML using PHP

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
geny
Forum Newbie
Posts: 4
Joined: Sat Jun 06, 2009 6:37 am

Extracting from XML using PHP

Post by geny »

Hi ,
There is this link -> http://data.giub.uni-bonn.de/openroutes ... tions=true

Where I want to extract 2 things (shown below in BOLD):
<xls:Instruction>Drive half left on Kaiserstraße</xls:Instruction>
<xls:distance value="284" uom="YD"/>

So,far I tried :
$url="url_just_shown_above";
$output = file_get_contents($url);
$xml = simplexml_load_string($output);
echo $xml->{'xls:RouteInstruction'}->{'xls:Instruction'}."<br />";

but it does not helps, for attribute,I used :
$xml = simplexml_load_file("url_just_shown_above");

foreach($xml->{'xls:distance[5]'}->attributes() as $a => $b)
{
echo $a,'="',$b,"\"</br>";
}

it shows nothing either.
The var_dump($xml); shows :
object(SimpleXMLElement)#1 (1) { ["@attributes"]=> array(1) { ["version"]=> string(3) "1.1" } }

Please help me extracting the BOLDed text from the XML file mentioned above..What wrong am I doing ? Any kind of guidance or CODE Snippet will be of great help..Will greatly appreciate the same.. Thanks a lot !
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Extracting from XML using PHP

Post by Eric! »

I don't know anything about the xml classes, but you could just treat the url like text and search/parse the bits you want. You can just work with the get_file_contents data directly using your variable $output.

Try using stripos to find the locations in the string for the start tag and end tag and then extract what is in between.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Extracting from XML using PHP

Post by requinix »

Eric! wrote:Try using stripos to find the locations in the string for the start tag and end tag and then extract what is in between.
Hacks like that are what give programmers a bad name. If you don't know how to do something then learn.


Use namespaces.

Whenever you see a tag name like <foo:bar> it means the "bar" element is a member of the "foo" namespace. By default, SimpleXML only tells you about elements in the default namespace (ie, <bar> or <xmlns:bar>).

In the XML there will be a URL defining that namespace. Normally it's found in the root as

Code: Select all

<root xmlns:foo="http://example.com/path/to/foo">
In your case it's slightly different:

Code: Select all

<xls:XLS version="1.1" xsi:schemaLocation="http://www.opengis.net/xls http://schemas.opengis.net/ols/1.1.0/RouteService.xsd">
The xsi:schemaLocation is what you want - use the first URL. Give it to SimpleXML's children() function.

Code: Select all

$xml = simplexml_load_file($url);
 
define("NS_XLS", "http://www.opengis.net/xls");
$ril = $xml->children(NS_XLS)->Response->DetermineRouteResponse->RouteInstructionsList;
foreach ($ril->RouteInstruction as $ri) {
    echo $ri->Instruction, "<br>\n";
}
geny
Forum Newbie
Posts: 4
Joined: Sat Jun 06, 2009 6:37 am

Re: Extracting from XML using PHP

Post by geny »

Thank you sooooooooo much !! It worked awesomely.. Greatfull to you :)
Post Reply