php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #25587 xml_parse_into_struct
Submitted: 2003-09-18 09:20 UTC Modified: 2003-10-02 17:26 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:3 (100.0%)
From: dan at ghul dot org Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 4.3.3 OS: any
Private report: No CVE-ID: None
 [2003-09-18 09:20 UTC] dan at ghul dot org
Description:
------------

this is a feature request: please consider adding/replacing the useless xlm_parse_into_struct by something more useful that returns a struct at least close to the DOM!

as you can see from posts all around, noone can really cope with the result of this parser and almost everyone goes ahead and builds his own parser to get a reasonable data struct. that's a waste of time.

there are plenty of solutions around that describe what people expect from a _parse_into_struct function. take Adam Tylmad's code for example. the result is squieky clean!! what about taking that and implementing it? you'd make lotsa people happy...

if you need xml_parse_into_struct for backwards compatibility, insert a new function that provides proper functionality.or then to quote a post from the manpage:
------
There are alot of reasons to make this functionality at least deprecated and replace it with a real XML parser. 
------



Reproduce code:
---------------
<?php 

class XMLParser { 
    var $path; 
    var $result; 

    function cleanString($string) { 
        return trim(str_replace("'", "&#39;", $string)); 
    } 
    
    function XMLParser($encoding, $data) { 
        $this->path = "\$this->result"; 
        $this->index = 0; 
        
        $xml_parser = xml_parser_create($encoding); 
        xml_set_object($xml_parser, &$this); 
        xml_set_element_handler($xml_parser, 'startElement', 'endElement'); 
        xml_set_character_data_handler($xml_parser, 'characterData'); 

        xml_parse($xml_parser, $data, true); 
        xml_parser_free($xml_parser); 
    } 
    
    function startElement($parser, $tag, $attributeList) { 
        $this->path .= "->".$tag; 
        eval("\$data = ".$this->path.";"); 
        if (is_array($data)) { 
            $index = sizeof($data); 
            $this->path .= "[".$index."]"; 
        } else if (is_object($data)) { 
            eval($this->path." = array(".$this->path.");"); 
            $this->path .= "[1]"; 
        } 

        foreach($attributeList as $name => $value) 
            eval($this->path."->".$name. " = '".XMLParser::cleanString($value)."';"); 
    } 
    
    function endElement($parser, $tag) { 
        $this->path = substr($this->path, 0, strrpos($this->path, "->")); 
    } 
    
    function characterData($parser, $data) { 
        if ($data = XMLParser::cleanString($data)) 
            eval($this->path." = '$data';"); 
    } 
} 

?> 


Expected result:
----------------
gdemartini's xml-example generates this structure: 

stdClass Object 
( 
    [FORM] => stdClass Object 
        ( 
            [SECTION] => stdClass Object 
                ( 
                    [NAME] => Data 
                    [EDITFIELD] => stdClass Object 
                        ( 
                            [LABEL] => Text: 
                            [NAME] => poll_text 
                            [LENGTH] => 255 
                            [SIZE] => 56 
                            [REQUIRED] => T 
                        ) 

                    [MEMOFIELD] => stdClass Object 
                        ( 
                            [LABEL] => Options 
                            [NAME] => options 
                            [COLS] => 56 
                            [ROWS] => 5 
                            [REQUIRED] => T 
                        ) 
                ) 
        ) 
) 

the moldb example generates this structure: 

stdClass Object 
( 
    [MOLDB] => stdClass Object 
        ( 
            [MOLECULE] => Array 
                ( 
                    [0] => stdClass Object 
                        ( 
                            [NAME] => Alanine 
                            [SYMBOL] => ala 
                            [CODE] => A 
                            [TYPE] => hydrophobic 
                        ) 

                    [1] => stdClass Object 
                        ( 
                            [NAME] => Lysine 
                            [SYMBOL] => lys 
                            [CODE] => K 
                            [TYPE] => charged 
                        ) 
                ) 
        ) 
) 


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-10-02 17:26 UTC] moriyoshi@php.net
simplexml extension, which will be brought into php5, is definitely what you are looking for.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 20 19:01:33 2024 UTC