thefalasifas
thefalasifas

Reputation: 267

Simple XML Load File with PHP

i have problem when parsing XML with simplexml_load_file method. I have xml like this one:

<root>
    <show>
        <list>
            <entry name="this is title" code="1234">
                <infos count="4">
                    <info type="random" percent="10" />
                    <info type="random" percent="12" />
                    <info type="random" percent="13" />
                    <info type="random" percent="15" />
                </infos>
            </entry>
            <entry name="this is titles" code="1235">
                <infos count="4">
                    <info type="random" percent="14" />
                    <info type="random" percent="16" />
                    <info type="random" percent="17" />
                    <info type="random" percent="18" />
                </infos>
            </entry>
        </list>
    </show>
</root>

And i trying parsing with simplexml_load_file, but i just get one result. Not two entry, i mean. Look at this code below.

<?php
$url = "text.xml";

$xml = simplexml_load_file($url);

foreach ($xml->show->list as $entry) {
$name = $entry->entry['name'];
$code = $entry->entry['code'];

echo "<p>The List Name: ".$name."</p>";
echo "<p>Code:".$code."</p>";
}
?> 

Okay, i try with that code, and i just get one result. I didn't know how to loop. So, i trying to ask in this site.

  1. How to loop the result exactly as the xml.
  2. Parsing XML with

Thanks for everybody who can give their hand :D

Upvotes: 0

Views: 9277

Answers (1)

Timmetje
Timmetje

Reputation: 7694

foreach ($xml->list->enrty as $entry) {
    $name = $entry->entry['name'];
    $code = $entry->entry['code'];

    echo "<p>The List Name: ".$name."</p>";
    echo "<p>Code:".$code."</p>";
}

Should be

foreach ($xml->show->list->entry as $entry) {
    $name = $entry->attributes()->name;
    $code = $entry->attributes()->code;
    //or like this
    //$name = $entry["name"];
    //$name = $entry["code"];
    echo "<p>The List Name: ".$name."</p>";
    echo "<p>Code:".$code."</p>";
}

Upvotes: 3

Related Questions