Current location: Hot Scripts Forums » Programming Languages » PHP » Extracting from XML using PHP


Extracting from XML using PHP

Reply
  #1 (permalink)  
Old 06-17-09, 11:03 AM
geny geny is offline
New Member
 
Join Date: Jun 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Extracting from XML using PHP

Hi ,
There is this link -> http://data.giub.uni-bonn.de/openrou...tructions=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 :
PHP Code:

$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 :
Code:
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 !

Last edited by Nico; 06-19-09 at 05:40 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-19-09, 07:38 AM
Wolf1994 Wolf1994 is offline
Wannabe Coder
 
Join Date: Sep 2005
Location: Russia
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
Why not to use a regular expressions?

Something like this:

Code:
preg_match_all ("/\<xls\:Instruction\>(.*)\<\/xls\:Instruction\>/U", $output, $match0);
print_r ($match0);

preg_match_all ('/\<xls\:distance value\=\"([0-9]+)\" uom\=\"YD\"\/\>/U', $output, $match1);
print_r ($match1);
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-19-09, 04:54 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Using your xml file, I'm getting namespace errors throughout the file using simpleXML.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 06-19-09, 05:46 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
PHP Code:

$xml simplexml_load_file($long_url);

print_r($xml->children('http://www.opengis.net/xls')); 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 06-19-09, 07:26 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Well the namespace files finally started working.

PHP Code:

<?php

echo '<pre>';
$xml simplexml_load_file("$PUTthatHUGExmlFILEurlHERE");

foreach(
$xml->xpath('//xls:RouteInstruction/*') as $v)
    {
        print (isset(
$v[0][0]->attributes()->value)) ? $v[0][0]->attributes()->value ' Kilometers <br/>' $v[0][0] . '<br/>';
    }


echo 
'</pre>';


?>
See if you like it...

Last edited by Jcbones; 06-19-09 at 07:31 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
XML PHP MYSQL- Writing to a xml file mackem PHP 3 04-07-09 09:55 PM
PHP Parser Code Needed (Shopping.com XML) angmi90 Job Offers & Assistance 1 11-18-08 12:46 PM
Embeding PHP code in XML?? ausgezeichnete PHP 7 01-28-08 06:38 AM
PHP, XML images. nightlord PHP 1 07-05-06 10:09 PM
php and xml pdaniel2004 PHP 0 11-01-04 09:02 AM


All times are GMT -5. The time now is 02:48 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.