/    Sign up×
Community /Pin to ProfileBookmark

XML parsing sucks….

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,

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @lpx spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...