0

I was considering making a menu using XML and Javascript. But I am not sure, how to. I was thinking something like this Menu.xml

<menuroot>
<menu src="house.png" link="index.htm">img</menu>
<menu src="news.png" link="news.htm">img</menu>
<menu link="index.htm">1-3
<submenu link="1.htm"> 1</submenu>
<submenu link="2.htm"> 2</submenu>
<submenu link="3.htm"> 3
<submenu>31</submenu>
<submenu>32</submenu>
</submenu>
</menuroot>

All I want is an easy way for people who dont know programming to change the menu. Is this a smart way of doing this? Should I try something else? Any ideas? I also know of PHP if thats something I should rather use.

3
  • I personnaly find xml more disturbing than html^^ Commented Mar 8, 2016 at 6:04
  • It depends upon your need that whether you should use XML here. XML is used when we need to work in API or some generic structure being predefined. Personally I suggest for menu preparation you can do with HTML itself. Why XML? Commented Mar 8, 2016 at 6:12
  • XML because I think it is easier for people who dont know HTML. Commented Mar 8, 2016 at 11:25

2 Answers 2

2

use the XML and Javascript

<p id="Menu"></p>
<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
        myFunction(xhttp);
    }
};
xhttp.open("GET", "Menu.xml", true);
xhttp.send();

function myFunction(xml) {
    var xmlDoc = xml.responseXML;
    document.getElementById("Menu").innerHTML =
    xmlDoc.getElementsByTagName("menu")[0].childNodes[0].nodeValue;
}
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

But this does not work for three levels in the menu? I have a <ul><il>-menu.
0

use the PHP function simplexml_load_file which will turn the XML into an object more..

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

2 Comments

Is it simple to print out in <ul><il>-menu for three levels?
yes you can use ul li menu like <ul><?php foreach($menu as $m){ ?><li><?php echo $m; ?></li><?php } ?></ul>

Your Answer

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