FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Load Data from XML url
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Load Data from XML url [message #183095] Tue, 08 October 2013 08:00 Go to next message
frank.catry is currently offline  frank.catry
Messages: 9
Registered: October 2013
Karma: 0
Junior Member
Hi,
how can i load the data from this URL
http://services.mobile.de/1.0.0/refdata/sites/GERMANY/classes/Car/makes
into a array or put them into a table?

I wish to retrieve the values of url, key and local-description.

Frank.
Re: Load Data from XML url [message #183096 is a reply to message #183095] Tue, 08 October 2013 11:25 Go to previous messageGo to next message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma: 0
Senior Member
On Tue, 8 Oct 2013 01:00:07 -0700 (PDT), frank(dot)catry(at)gmail(dot)com wrote:
> Hi,
> how can i load the data from this URL
> http://services.mobile.de/1.0.0/refdata/sites/GERMANY/classes/Car/makes
> into a array or put them into a table?
>
> I wish to retrieve the values of url, key and local-description.

http://php.net/manual/en/function.xml-parse-into-struct.php

--
Don't eat things hawked by cartoon characters or people concealing their
identities, including masks or clown makeup. It doesn't end well.
Re: Load Data from XML url [message #183114 is a reply to message #183096] Wed, 09 October 2013 19:51 Go to previous messageGo to next message
frank.catry is currently offline  frank.catry
Messages: 9
Registered: October 2013
Karma: 0
Junior Member
Op dinsdag 8 oktober 2013 13:25:16 UTC+2 schreef Peter H. Coffin:
> On Tue, 8 Oct 2013 01:00:07 -0700 (PDT), wrote:
>
>> Hi,
>
>> how can i load the data from this URL
>
>> http://services.mobile.de/1.0.0/refdata/sites/GERMANY/classes/Car/makes
>
>> into a array or put them into a table?
>
>>
>
>> I wish to retrieve the values of url, key and local-description.
>
>
>
> http://php.net/manual/en/function.xml-parse-into-struct.php
>
>
>
> --
>
> Don't eat things hawked by cartoon characters or people concealing their
>
> identities, including masks or clown makeup. It doesn't end well.

Hi Peter, problem is that the result of this url seems to be NOT XML because there are nodes / childs returned. Did you look at the XML ?
Re: Load Data from XML url [message #183115 is a reply to message #183114] Wed, 09 October 2013 20:23 Go to previous messageGo to next message
Fiver is currently offline  Fiver
Messages: 35
Registered: July 2013
Karma: 0
Member
On 2013-10-09 21:51, Frank Catry wrote:
> Op dinsdag 8 oktober 2013 13:25:16 UTC+2 schreef Peter H. Coffin:
>> On Tue, 8 Oct 2013 01:00:07 -0700 (PDT), wrote:
>>> how can i load the data from this URL
>>> http://services.mobile.de/1.0.0/refdata/sites/GERMANY/classes/Car/makes
>>> into a array or put them into a table?
>>
>>> I wish to retrieve the values of url, key and local-description.
>>
>> http://php.net/manual/en/function.xml-parse-into-struct.php

> Hi Peter, problem is that the result of this url seems to be NOT XML
> because there are nodes / childs returned. Did you look at the XML ?

Huh? Look at the XML that is not XML?

It's XML alright. Here's something to get you started:

$doc = new DOMDocument();
$doc->load("http://services.mobile.de/ ...yadda...");

$result = [];
$items = $doc->documentElement->getElementsByTagName("item");
foreach ($items as $item) {
$result[] =[
"key" => $item->getAttribute("key"),
"url" => $item->getAttribute("url"),
"description" => $item->textContent,
];
}

print_r($result);

Assumes you can open remote files (i.e. that allow_url_fopen is enabled)
and that the XML namespaces are not important for your task.

regards,
5er
Re: Load Data from XML url [message #183132 is a reply to message #183115] Thu, 10 October 2013 07:38 Go to previous messageGo to next message
frank.catry is currently offline  frank.catry
Messages: 9
Registered: October 2013
Karma: 0
Junior Member
Thank you very much. Your script was not working at once so i do some changes and get it work in a few minutes.

<?php
$doc = new DOMDocument();
$doc->load("http://services.mobile.de/1.0.0/refdata/sites/GERMANY/classes/Car/makes");

$items = $doc->documentElement->getElementsByTagName("item");

foreach ($items as $item) {
$key=$item->getAttribute("key");
$description=$item->textContent;
echo "$key - $description<br>";
}

die(date('l jS \of F Y h:i:s A'));
?>
Maybe not so professional or fast as your solution but it works for me.
The values are only to load once a week into a database.
Re: Load Data from XML url [message #183198 is a reply to message #183132] Sat, 12 October 2013 04:17 Go to previous messageGo to next message
Fiver is currently offline  Fiver
Messages: 35
Registered: July 2013
Karma: 0
Member
On 2013-10-10 09:38, Frank Catry wrote:
> Thank you very much. Your script was not working at once so i do
> some changes and get it work in a few minutes.
>
> <?php
> $doc = new DOMDocument();
> $doc->load("http://services.mobile.de/1.0.0/refdata/sites/GERMANY/classes/Car/makes");
>
> $items = $doc->documentElement->getElementsByTagName("item");
>
> foreach ($items as $item) {
> $key=$item->getAttribute("key");
> $description=$item->textContent;
> echo "$key - $description<br>";
> }
>
> die(date('l jS \of F Y h:i:s A'));
> ?>
> Maybe not so professional or fast as your solution but it works for me.
> The values are only to load once a week into a database.

Looking good.

I'm guessing that the reason why my version didn't run for you was this:

$result = [];
..
$result[] = [ ..... ];

That's PHP 5.4 syntax. Sorry, I got used to it so quickly that I keep
forgetting about backward compatibility. In 5.3 you'd write:

$result = array();
..
$result[] = array( ..... );

Everything else should work fine in 5.3, I think.


Ah, one more thing: the "->documentElement" part can be removed, it
doesn't do anything. That was accidentally left in the example while I
was trying something else.


regards,
5er
Re: Load Data from XML url [message #183231 is a reply to message #183095] Sun, 13 October 2013 16:16 Go to previous messageGo to next message
frank.catry is currently offline  frank.catry
Messages: 9
Registered: October 2013
Karma: 0
Junior Member
Hi 5er,

indeed this line was not working.

Everthings works fine execpt i have a problem with getting some special characters right in the database.

'Düvelsdor' in the XML results in 'Dücker' when loading in the DOM object and appears in this way in the database ??

Do you know a solution ?

Frank.
Re: Load Data from XML url [message #183309 is a reply to message #183231] Sun, 20 October 2013 14:08 Go to previous message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
Frank Catry, 2013-10-13 18:16:

> Hi 5er,
>
> indeed this line was not working.
>
> Everthings works fine execpt i have a problem with getting some special characters right in the database.
>
> 'Düvelsdor' in the XML results in 'Dücker' when loading in the DOM object and appears in this way in the database ??

Seems to be UTF-8.

> Do you know a solution ?

Use the proper encoding.


--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: glob patterns
Next Topic: how to change character to % character?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 21:44:03 GMT 2024

Total time taken to generate the page: 0.02184 seconds