XML with PHP – make links easily

I really did not think to make a post about it, but as far  as I have lost about 15 minutes in research how to do it, it is obviously that spending some 10 minutes more would be valuable.

The idea is to have a php file with a web link and to produce a simple list from it in PHP. Something trivial, if you do not forget the rule for the xml lists – they should all be with one root, otherwise there is an error.
links_php

So here are the two files the test.php and the example.xml, that provide the abovementioned result. Enjoy them:

<?php

$xml=simplexml_load_file("example.xml");
echo "<ul>\n";

foreach($xml->user as $unit) {
    $my_name = htmlspecialchars((string)$unit->name);
    $my_href =  htmlspecialchars((string)$unit->website);
    echo "<li><a href=\"$my_href\">$my_name</a></li>\n";
    }
    echo "</ul>\n";
?>
<?xml version="1.0"?>
<users>
    <user>
        <name>Vitosh</name>
        <website>https://www.vitoshacademy.com</website>
    </user>
    <user>
        <name>Hater</name>
        <website>http://www.hategame.com</website>
    </user>
    <user>
        <name>Business Vitosh</name>
        <website>http://www.vit-consulting.com</website>
    </user>
</users>

Available also in GitHub! 🙂