Hi,
I’m newbie to XML parsing and i passed the day trying to figure out how could I parse a simple XML file to a array.
The objective is to put a XML file with the translations for each constant word of my website to other languages.
<list>
<languages type=”portuguese”>
<click>Clica aqui</click>
<newsletter>Assine a newsletter</newsletter>
<details>Ver detalhes</details>
<news>NOTICIAS</news>
<next>PROXIMO EVENTO</next>
<last>ULTIMO EVENTO</last>
<featured>ARTISTA EM DESTAQUE</featured>
</languages>
<languages type=”english”>
<click>Click here</click>
<newsletter>Sign the newsletter</newsletter>
<details>Check details</details>
<news>NEWS</news>
<next>NEXT EVENT</next>
<last>LAST EVENT</last>
<featured>FEATURED ARTIST</featured>
</languages>
</list>
I just want to put the information on a array like this
$portuguese = array(“click” => “Clique aqui”, “home” => “Página inicial”, …);
$english = array(“click” => “Click here”, “home” => “Homepage”, …);
The example I was trying is this one:
require_once ‘modules/array_combine.php’;
$elements = array();
$portuguese = array();
$english = array();
$flag = “”;
function opening_element($parser, $element, $attributes) {
global $flag;
global $elements;
if ($element == “languages”) {
$flag = $attributes[“type”];
}
else if ($element != “list” && !array_search($element,$elements))
$elements[] = $element;
}
function closing_element($parser, $element) {
global $flag;
if ($element == “languages”) {
$flag = “”;
}
}
function character_data($parser, $data) {
global $flag;
if ($flag == “portuguese”) {
global $portuguese;
$portuguese[] = $data;
}
if ($flag == “english”) {
global $english;
$english[] = $data;
}
}
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($parser, “opening_element”, “closing_element”);
xml_set_character_data_handler($parser, “character_data”);
$document = file(“languages/languages.xml”);
foreach ($document as $line) {
xml_parse($parser, $line);
}
$aux = array();
xml_parser_free($parser);
Because i don’t know to well the ins and outs of XML parsing i was trying to put the tags in an array and the values in other to then combine in a single one, but i don’t know why, the array of the tags has a lot of more elements than tags and strangely when i print them they seem to me the right ones.
This is driving me crazy.
Help please. ?
Cheers,