the part of PHP code i referred to (with altered variable names) is
$lang = "US";
...
global $lang;
php code:
<?php
include_once("menus.php");
$xml_file = "menu.xml";
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "charData");
$fp = fopen($xml_file, "r")
or die("Error opening XML file");
while ($data = fread($fp, 4096))
{
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
}
fclose($fp);
xml_parser_free($xml_parser);
$lang = "US";
$insideMenu = false;
$insideItem = false;
$tag = "";
$title = "";
$section = "";
$page = "";
$menuId = "";
$itemId = "";
function startElement($parser, $tagName, $attrs)
{
global $insideMenu, $insideItem, $tag, $menuId, $itemId;
global $lang;
echo "country: ".$lang."<BR>";
// echo "insideMenu: ".$insideMenu."<BR>";
//echo "found {$tagName}<BR>";
if ($insideMenu)
{
if ($tag == $lang)
{
//echo "found ".$tag."<BR>";
$tag .= "*".$tagName;
}
else
$tag = $tagName;
if ($tagName == "ITEM")
{
$insideItem = true;
$itemId = $attrs["id"];
}
}
else if ($tagName == "MENU")
{
$insideMenu = true;
$menuId = $attrs["name"];
}
}
function charData($parser, $data)
{
global $insideMenu, $insideItem, $tag, $title, $section, $page, $ID;
//echo "tag: ".$tag."<BR>";
if ($insideMenu)
{
if ($insideItem)
{
switch ($tag)
{
case "SHORT":
echo "SHORT<BR>";
break;
case "LONG":
break;
case "SHORT*US":
echo $data."<BR>";
break;
}
}
else
{
switch ($tag)
{
case "TITLE" :
$title = $data;
break;
}
}
}
}
function endElement($parser, $tagName)
{
global $insideItem, $insideMenu;
//echo "end tag {$tagName}<BR>";
if ($tagName == "ITEM")
{
$insideItem = false;
}
else if ($tagName == "MENU")
$insideMenu = false;
}
?>
xml sample:
<?xml version="1.0" encoding="ISO-8859-1"?>
<menus>
<menu name="home">
<title>Home</title>
<item id="home">
<short>
<us>home</us>
<tw></tw>
</short>
</item>
<item id="news">
<short>
<us>news</us>
<tw></tw>
</short>
</item>
<item id="contact">
<section>about</section>
<short>
<us>contact</us>
<tw></tw>
</short>
<long>
<us>contact us</us>
<tw></tw>
</long>
</item>
</menu>
</menus>