2

I'm trying to loop multiple items from an XML with the code below.

$xml = get_data('the-url');
$data = simplexml_load_string($xml);

foreach($data->item AS $item) {
    foreach($item AS $test) {
        var_dump($test);
    }
}

The XML looks like this:

<xml>
    <item>
        <name>...</name>
        <chapter>...</chapter>
        <text>
            ...
        </text>
    </item>
    <item>
        <name>...</name>
        <chapter>...</chapter>
        <text>
            ...
        </text>
    </item>
    <item>
        <name>...</name>
        <chapter>...</chapter>
        <text>
            ...
        </text>
    </item>
    <item>
        <name>...</name>
        <chapter>...</chapter>
        <text>
            ...
        </text>
    </item>
    <item>
        <name>...</name>
        <chapter>...</chapter>
        <text>
            ...
        </text>
    </item>
</xml>

As it is right now, the var_dump($test) prints this:

object(SimpleXMLElement)#7 (0) { } object(SimpleXMLElement)#8 (0) { }
object(SimpleXMLElement)#7 (0) { } object(SimpleXMLElement)#8 (0) { }
object(SimpleXMLElement)#7 (0) { } object(SimpleXMLElement)#8 (0) { }
object(SimpleXMLElement)#7 (0) { } object(SimpleXMLElement)#8 (0) { }
object(SimpleXMLElement)#7 (0) { } object(SimpleXMLElement)#8 (0) { }
object(SimpleXMLElement)#7 (0) { } object(SimpleXMLElement)#8 (0) { }
object(SimpleXMLElement)#7 (0) { } object(SimpleXMLElement)#8 (0) { } ...

What should I do to make my code to loop all the items and prints the value?

Airikr
  • 6,258
  • 15
  • 59
  • 110

1 Answers1

0

Try this:

$xml_content = file_get_contents('http://url/feed.xml');
$data = simplexml_load_string($xml_content);

foreach ($data->item as $item) {
    var_dump($item->bookname);
}

You can also cast whole $item to array if you need:

foreach ($data->item as $item) {
    $itemArray = (array) $item;
    var_dump($itemArray);
}

You were dumping objects, when you need to cast the to string first to return the text value. Also, I do not think the second foreach was doing anything or needed, so I removed it.

ek9
  • 3,392
  • 5
  • 23
  • 34
  • Thanks but I got this error message: `Warning: simplexml_load_file(): I/O warning : failed to load external entity ...`. I tried with my own function `get_data()` and after that with `file_get_contents()` as you used before your last edit. I'm trying to load this XML and get the values: http://labs.bible.org/api/?passage=Leviticus%2024&type=xml – Airikr Mar 15 '14 at 17:31
  • answer updated; `simplexml_load_file()` is only for local files it seems – ek9 Mar 15 '14 at 17:34
  • `var_dump($item->toString());` gives me `Fatal error: Call to undefined method SimpleXMLElement::toString() in ...` while `var_dump((string)$item);` gives me `string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" ...` – Airikr Mar 15 '14 at 17:36
  • Same result as before: `string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) ""`. I copy-pasted your code – Airikr Mar 15 '14 at 17:40
  • check it out now, I have tested it on my own environment now, and it works. – ek9 Mar 15 '14 at 17:48