0

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

0

1 Answer 1

3
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>";
}
Sign up to request clarification or add additional context in comments.

2 Comments

yeah, it's work, but just show one entry. how to show all of the rest entry?
$xml->show->list->entry Dont forget ->show-> phpfiddle.org/lite/code/xaf-ecu

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.