0

What I want to do is create links for my site according to an XML document. example:

<menu>
  <item>
    <name>Menu Item One</name>
    <itemurl>folder\location\page.php</itemurl>
    <caption>This text to be applied to the 'title' attribute</caption>
  </item>
...
</menu>

I want each 'item' to be a link on the site. The menu will be its own page incorporated into the site with the 'include' function. This way, I just edit the XML file to add/remove menu items. If I need to format the XML differently is not a big deal, especially if it can be done easier than what I am thinking. I want the navigation menu to be at the very top of the page inside a table with only a single row and each link inside its own datacell. For test purposes, all of the files will be in a common folder; index.php (includes the menu.php), menu.php, menu.xml... I am not sure what other information is needed.

The outcome for each item should ideally be something like:

<td class="menuitem">
  <a href=$ItemURL title=$Caption>$ItemName</a>
</td>

ANY help on this topic would be greatly appreciated. I have found some text on PHP and XML but cannot figure out this specific task. Sorry to all moderators if I have mis-posted somehow.

2 Answers2

0

You want to use the PHP function simplexml_load_file which will turn the XML into an object.

http://php.net/manual/en/function.simplexml-load-file.php

<?php
$myMenu = simplexml_load_file('menu.xml');
echo $myMenu->item[0]->itemurl;
?>

You can loop through the items with a foreach.

Don Rhummy
  • 24,730
  • 42
  • 175
  • 330
0

You can make use of PHP as a templating engine, and load the xml file before you display it in form of a multi-dimensional object with iteration capabilities like it's the case with SimpleXMLElement:

$menu = simplexml_load_file('menu.xml');
require('menu_script.php');

In the menu-script you just access the $menu variable for display purposes:

<table>
    <tr>
        <?php foreach($menu->item as $item) { ?>
            <td class="menuitem">
                <a href="<?= htmlspecialchars($item->itemurl); ?>"
                   title="<?= htmlspecialchars($item->caption); ?>"
                >
                    <?= htmlspecialchars($item->name); ?>
                </a>
            </td>
        <?php } ?>
    </tr>
</table>

This can be further improved by wrapping the requiring of the menu view script so that only local variables are accessible (see for example: Is include()/require() with “side effects” a bad practice?).

Additionally the view object $menu could be decorated to automatically provide properly encoded data for usage in HTML. Decorating SimpleXMLElement is not well covered on Stackoverflow itself but referenced in some SimpleXML related questions.

As an alternative to roll out all this your own, I could imagine that a simple template language like mustache could work pretty well with SimpleXMLElement automatically providing properly encoded data out of the box or with less typing (mustache has some escape features), but I have never tested that, so that's just a suggestion to sandbox and to outline that you should look for existing things as well.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836