Results 1 to 3 of 3

Thread: XML Convertor broken for some reason...

  1. #1

    Thread Starter
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765

    XML Convertor broken for some reason...

    Grumble, ok, here comes a pile of code that I picked up from PHP.net

    PHP Code:
        function &last(&$array) {
                    
    error_reporting(E_ALL);
            if (!
    count($array)) return null;
            
    end($array);
            return 
    $array[key($array)];
        }

        function 
    myParseXML(&$vals, &$dom, &$lev) {
                    
    error_reporting(E_ALL);
            do {
                
    $curr current($vals);
                
    $lev $curr['level'];
                
                switch (
    $curr['type']) {
                    case 
    'open':
                        if (isset(
    $dom[$curr['tag']])) {
                            
    $tmp $dom[$curr['tag']];
                            if (!@
    $tmp['__multi'])
                                
    $dom[$curr['tag']] = array('__multi' => true$tmp);
                            
    array_push($dom[$curr['tag']], array());
                            
    $new =& last($dom[$curr['tag']]);
                        } else {
                            
    $dom[$curr['tag']] = array();
                            
    $new =& $dom[$curr['tag']];
                        }
                        
    next($vals);
                        
    //REMOVED A Pointer
                        
    myParseXML($vals$new$lev);
                        break;
                    case 
    'cdata':
                        break;
                    case 
    'complete':
                        if (!isset(
    $dom[$curr['tag']]))
                            
    $dom[$curr['tag']] = $curr['value'];
                        else {
                            if (
    is_array($dom[$curr['tag']])) 
                                
    array_push($dom[$curr['tag']] , $curr['value']);
                            else
                                
    array_push($dom[$curr['tag']] = array($dom[$curr['tag']]) , $curr['value']);
                        }
                        break;
                    case 
    'close':
                        return;
                }
            } while (
    next($vals)!==FALSE);
        }

        function 
    MyXMLtoArray($XML) {
            
    error_reporting(E_ALL);
            
            
    $xml_parser xml_parser_create();
            
    xml_parse_into_struct($xml_parser$XML$vals);
            
    xml_parser_free($xml_parser);
            
    reset($vals);
            
            
    $dom = array(); $lev 0;
            
            
    myParseXML($vals$dom$lev);
            
            return 
    $dom;
        } 
    MyXMLToArray is the starting point, you pass it a string containing XML and its supposed to return a nice array that I can loop with. This code was working perfectly last year, and is now stuffed. :\ Any ideas as to why? It simple returns a blank page.. I've been using comments and I believe the cause is MyXMLParse, but not sure why...

    If no-one can figure out what its doing wrong, perhaps some alternative code that works well... going to be a complete pain in the arse to re-write my script though if I have to go down that route
    Last edited by Pc_Madness; Feb 8th, 2006 at 02:05 AM.
    Don't Rate my posts.

  2. #2
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: XML Convertor broken for some reason...

    I found this on one of the comments on php.net it says it is a modified version of a previous function that does what you want

    Quote Originally Posted by http://uk2.php.net/xml
    For anyone else looking for a xml parser not requiring a library, here's a modified version of xml2array, which returns a nested associative array, including attributes and nested nodes. (BTW, thanks to whoever originally wrote these regexes - they're quite clever)
    PHP Code:
    <?
    function xml2array ($xml)
    {
       $xmlary = array ();

       if ((strlen ($xml) < 256) && is_file ($xml))
         $xml = file_get_contents ($xml);
     
       $ReElements = '/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*)<\/\s*\\1\s*>)/s';
       $ReAttributes = '/(\w+)=(?:"|\')([^"\']*)(:?"|\')/';
     
       preg_match_all ($ReElements, $xml, $elements);
       foreach ($elements[1] as $ie => $xx) {
       $xmlary[$ie]["name"] = $elements[1][$ie];
         if ( $attributes = trim($elements[2][$ie])) {
             preg_match_all ($ReAttributes, $attributes, $att);
             foreach ($att[1] as $ia => $xx)
               // all the attributes for current element are added here
               $xmlary[$ie]["attributes"][$att[1][$ia]] = $att[2][$ia];
         } // if $attributes
        
         // get text if it's combined with sub elements
       $cdend = strpos($elements[3][$ie],"<");
       if ($cdend > 0) {
               $xmlary[$ie]["text"] = substr($elements[3][$ie],0,$cdend -1);
           } // if cdend
          
         if (preg_match ($ReElements, $elements[3][$ie]))       
             $xmlary[$ie]["elements"] = xml2array ($elements[3][$ie]);
         else if ($elements[3][$ie]){
             $xmlary[$ie]["text"] = $elements[3][$ie];
             }
       }
       return $xmlary;
    }
    ?>

    Parsing this:
    <XML>
    <header>
    <title> Sample App </title>
    <version> v. 1.0</version>
    </header>
      <window height='220' width='420'>
       stuff in the window
      </window>
    </XML>

    returns a print_r of this:
    Array
    (
       [0] => Array
           (
               [name] => XML
               [text] =>
               [elements] => Array
                   (
                       [0] => Array
                           (
                               [name] => header
                               [text] =>
                               [elements] => Array
                                   (
                                       [0] => Array
                                           (
                                               [name] => title
                                               [text] =>  Sample App
                                           )

                                       [1] => Array
                                           (
                                               [name] => version
                                               [text] =>  v. 1.0
                                           )

                                   )

                           )

                       [1] => Array
                           (
                               [name] => window
                               [attributes] => Array
                                   (
                                       [height] => 220
                                       [width] => 420
                                   )

                               [text] =>
       stuff in the window
     
                           )

                   )

           )

    )

  3. #3

    Thread Starter
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765

    Re: XML Convertor broken for some reason...

    Hmm, alrightie I think I can get it going.. the script doesn't seem to like me declaring multiple <subject></subject> elements.. and tries to overwrite them Shall figure it out.
    Don't Rate my posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width