bardockyo
bardockyo

Reputation: 1435

Printing values from an XML file

XML file (ids is not the root):

<id_list>
    <ids>
        <id>185903535</id>
        <id>235450977</id>
        <id>274135256</id>
    </ids>
</id_list>

I am trying to print all the values in the XML file. I am very new to PHP and all I can print is the first element. Here is what I have so far:

<?php
$xml=simplexml_load_file('txt.xml');//php5
echo $xml->ids->id;
?>

I am sure it is something very simple and I've tried searching google but I am not exactly sure how to word what I am looking for. Thanks!

Upvotes: 0

Views: 47

Answers (1)

user142162
user142162

Reputation:

To access all of the <id> nodes, simply iterate over the $xml->ids->id object:

$xml = simplexml_load_file('txt.xml');
foreach ($xml->ids->id as $id) {
    echo $id;
}

Relevant documentation

Upvotes: 2

Related Questions