www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Server-Side Development > PHP

    PHP Discussion and technical support for using and deploying PHP based websites.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 05-07-2005, 03:14 PM
    lpx lpx is offline
    Registered User
     
    Join Date: Nov 2004
    Posts: 3
    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,
    __________________
    www.imaginando.net
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 12:03 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.