0

I try to figure out, how to extract an dropdownlist of the following xml data via simplexml.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<reference:reference xmlns:resource="http://services.mobile.de/schema/resource" xmlns:ad="http://services.mobile.de/schema/ad" xmlns:seller="http://services.mobile.de/schema/seller" xmlns:financing="http://services.mobile.de/schema/common/financing-1.0" xmlns:reference="http://services.mobile.de/schema/reference">
<reference:item key="ABARTH" url="http://services.mobile.de/1.0.0/refdata/classes/Car/makes/ABARTH">
    <resource:local-description xml-lang="en">Abarth</resource:local-description>
</reference:item>
<reference:item key="AC" url="http://services.mobile.de/1.0.0/refdata/classes/Car/makes/AC">
    <resource:local-description xml-lang="en">AC</resource:local-description>
</reference:item>

I need the HTML like this:

<select>
<option value=„reference:item->key“>resource:local-description</option>
</select>

Everything I try ends up in a fatal php error. My suggestion is, that the - in local-description prevents the code to run correctly.

i. e. this code:

$properties = $content->children('reference', TRUE)->item->children('resource', TRUE);
echo $properties->local-description;

Has somebody some hints for me?

2 Answers2

1

Your XML is not valid, it is missing the closing </reference:reference> tag.

use xpath to select the namespaced data:

$xml = simplexml_load_string($x); // assume XML in $x

$keys = $xml->xpath("//reference:item/@key");
$descs = $xml->xpath("//resource:local-description");

Now, in order to generate the output, do:

$c = count($keys);
$i = 0;

for ($i = 0; $i < $c; $i++)
    echo "key: $keys[$i] - value: $descs[$i]" . PHP_EOL;

See it working: https://eval.in/106207

michi
  • 6,565
  • 4
  • 33
  • 56
0

May be you should parse all the xml data by calling a function which do that stuff in AJAX. Then, in this function you send a response with datas you want and append these data with JS in your DOM.

I found this post when I needed it: convert xml into array

Community
  • 1
  • 1
KeizerBridge
  • 2,707
  • 7
  • 24
  • 37