|
|
|
Re: Load Data from XML url [message #183115 is a reply to message #183114] |
Wed, 09 October 2013 20:23 |
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 |
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 |
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 |
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 |
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
|
|
|