PHP XML2Array: Unterschied zwischen den Versionen

Aus ITwiki
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: Hier eine Klasse um in PHP eine XML Datei in ein Array einzulesen. <?php class xml2array { function xmlize($data, $WHITE=1) { $data = trim($data); ...)
 
K
 
(3 dazwischenliegende Versionen von 2 Benutzern werden nicht angezeigt)
Zeile 1: Zeile 1:
 
Hier eine Klasse um in PHP eine XML Datei in ein Array einzulesen.
 
Hier eine Klasse um in PHP eine XML Datei in ein Array einzulesen.
  
<?php
+
<source lang="php">
class xml2array
+
<?php
{
+
class xml2array
function xmlize($data, $WHITE=1) {
+
{
+
function xmlize($data, $WHITE=1) {
    $data = trim($data);
+
    $vals = $index = $array = array();
+
    $data = trim($data);
    $parser = xml_parser_create();
+
    $vals = $index = $array = array();
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
+
    $parser = xml_parser_create();
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $WHITE);
+
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    if ( !xml_parse_into_struct($parser, $data, $vals, $index) )
+
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $WHITE);
    {
+
    if ( !xml_parse_into_struct($parser, $data, $vals, $index) )
die(sprintf("XML error: %s at line %d",
+
    {
                    xml_error_string(xml_get_error_code($parser)),
+
die(sprintf("XML error: %s at line %d",
                    xml_get_current_line_number($parser)));
+
                    xml_error_string(xml_get_error_code($parser)),
+
                    xml_get_current_line_number($parser)));
    }
+
    xml_parser_free($parser);
+
    }
+
    xml_parser_free($parser);
    $i = 0;  
+
+
    $i = 0;  
    $tagname = $vals[$i]['tag'];
+
    if ( isset ($vals[$i]['attributes'] ) )
+
    $tagname = $vals[$i]['tag'];
    {
+
    if ( isset ($vals[$i]['attributes'] ) )
        $array[$tagname]['@'] = $vals[$i]['attributes'];
+
    {
    } else {
+
        $array[$tagname]['@'] = $vals[$i]['attributes'];
        $array[$tagname]['@'] = array();
+
    } else {
    }
+
        $array[$tagname]['@'] = array();
+
    }
    $array[$tagname]["#"] = $this->xml_depth($vals, $i);
+
+
    $array[$tagname]["#"] = $this->xml_depth($vals, $i);
    return $array;
+
}
+
    return $array;
+
}
/*  
+
*
+
/*  
* You don't need to do anything with this function, it's called by
+
*
* xmlize.  It's a recursive function, calling itself as it goes deeper
+
* You don't need to do anything with this function, it's called by
* into the xml levels.  If you make any improvements, please let me know.
+
* xmlize.  It's a recursive function, calling itself as it goes deeper
*
+
* into the xml levels.  If you make any improvements, please let me know.
*
+
*
*/
+
*
+
*/
function xml_depth($vals, &$i) {  
+
    $children = array();  
+
function xml_depth($vals, &$i) {  
+
    $children = array();  
    if ( isset($vals[$i]['value']) )
+
    {
+
    if ( isset($vals[$i]['value']) )
        array_push($children, $vals[$i]['value']);
+
    {
    }
+
        array_push($children, $vals[$i]['value']);
+
    }
    while (++$i < count($vals)) {  
+
+
    while (++$i < count($vals)) {  
        switch ($vals[$i]['type']) {  
+
+
        switch ($vals[$i]['type']) {  
          case 'open':  
+
+
          case 'open':  
                if ( isset ( $vals[$i]['tag'] ) )
+
                {
+
                if ( isset ( $vals[$i]['tag'] ) )
                    $tagname = $vals[$i]['tag'];
+
                {
                } else {
+
                    $tagname = $vals[$i]['tag'];
                    $tagname = '';
+
                } else {
                }
+
                    $tagname = '';
+
                }
                if ( isset ( $children[$tagname] ) )
+
                {
+
                if ( isset ( $children[$tagname] ) )
                    $size = sizeof($children[$tagname]);
+
                {
                } else {
+
                    $size = sizeof($children[$tagname]);
                    $size = 0;
+
                } else {
                }
+
                    $size = 0;
+
                }
                if ( isset ( $vals[$i]['attributes'] ) ) {
+
                    $children[$tagname][$size]['@'] = $vals[$i]["attributes"];
+
                if ( isset ( $vals[$i]['attributes'] ) ) {
                }
+
                    $children[$tagname][$size]['@'] = $vals[$i]["attributes"];
+
                }
                $children[$tagname][$size]['#'] = $this->xml_depth($vals, $i);
+
+
                $children[$tagname][$size]['#'] = $this->xml_depth($vals, $i);
            break;  
+
+
            break;  
+
            case 'cdata':
+
                array_push($children, $vals[$i]['value']);  
+
            case 'cdata':
            break;  
+
                array_push($children, $vals[$i]['value']);  
+
            break;  
            case 'complete':  
+
                $tagname = $vals[$i]['tag'];
+
            case 'complete':  
+
                $tagname = $vals[$i]['tag'];
                if( isset ($children[$tagname]) )
+
                {
+
                if( isset ($children[$tagname]) )
                    $size = sizeof($children[$tagname]);
+
                {
                } else {
+
                    $size = sizeof($children[$tagname]);
                    $size = 0;
+
                } else {
                }
+
                    $size = 0;
+
                }
                if( isset ( $vals[$i]['value'] ) )
+
                {
+
                if( isset ( $vals[$i]['value'] ) )
                    $children[$tagname][$size]["#"] = $vals[$i]['value'];
+
                {
                } else {
+
                    $children[$tagname][$size]["#"] = $vals[$i]['value'];
                    $children[$tagname][$size]["#"] = '';
+
                } else {
                }
+
                    $children[$tagname][$size]["#"] = '';
+
                }
                if ( isset ($vals[$i]['attributes']) ) {
+
                    $children[$tagname][$size]['@']
+
                if ( isset ($vals[$i]['attributes']) ) {
                                            = $vals[$i]['attributes'];
+
                    $children[$tagname][$size]['@']
                }
+
                                            = $vals[$i]['attributes'];
+
                }
            break;  
+
+
            break;  
            case 'close':
+
                return $children;  
+
            case 'close':
            break;
+
                return $children;  
        }  
+
            break;
+
        }  
    }  
+
+
    }  
return $children;
+
+
return $children;
}
+
+
}
function traverse_xmlize($array, $arrName = "array", $level = 0) {
+
+
function traverse_xmlize($array, $arrName = "array", $level = 0) {
    foreach($array as $key=>$val)
+
    {
+
    foreach($array as $key=>$val)
        if ( is_array($val) )
+
    {
        {
+
        if ( is_array($val) )
            traverse_xmlize($val, $arrName . "[" . $key . "]", $level + 1);
+
        {
        } else {
+
            traverse_xmlize($val, $arrName . "[" . $key . "]", $level + 1);
            $GLOBALS['traverse_array'][] = '$' . $arrName . '[' . $key . '] = "' . $val . "\"\n";
+
        } else {
        }
+
            $GLOBALS['traverse_array'][] = '$' . $arrName . '[' . $key . '] = "' . $val . "\"\n";
    }
+
        }
+
    }
    return 1;
+
+
    return 1;
}
+
}
+
}
?>
+
}
 +
?>
 +
</source>
 +
 
 +
[[Kategorie:PHP]]

Aktuelle Version vom 25. Februar 2010, 15:47 Uhr

Hier eine Klasse um in PHP eine XML Datei in ein Array einzulesen.

<?php
class xml2array
{
	function xmlize($data, $WHITE=1) {
	
	    $data = trim($data);
	    $vals = $index = $array = array();
	    $parser = xml_parser_create();
	    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
	    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $WHITE);
	    if ( !xml_parse_into_struct($parser, $data, $vals, $index) )
	    {
		die(sprintf("XML error: %s at line %d",
	                    xml_error_string(xml_get_error_code($parser)),
	                    xml_get_current_line_number($parser)));
	
	    }
	    xml_parser_free($parser);
	
	    $i = 0; 
	
	    $tagname = $vals[$i]['tag'];
	    if ( isset ($vals[$i]['attributes'] ) )
	    {
	        $array[$tagname]['@'] = $vals[$i]['attributes'];
	    } else {
	        $array[$tagname]['@'] = array();
	    }
	
	    $array[$tagname]["#"] = $this->xml_depth($vals, $i);
	
	    return $array;
	}
	
	/* 
	 *
	 * You don't need to do anything with this function, it's called by
	 * xmlize.  It's a recursive function, calling itself as it goes deeper
	 * into the xml levels.  If you make any improvements, please let me know.
	 *
	 *
	 */
	
	function xml_depth($vals, &$i) { 
	    $children = array(); 
	
	    if ( isset($vals[$i]['value']) )
	    {
	        array_push($children, $vals[$i]['value']);
	    }
	
	    while (++$i < count($vals)) { 
	
	        switch ($vals[$i]['type']) { 
	
	           case 'open': 
	
	                if ( isset ( $vals[$i]['tag'] ) )
	                {
	                    $tagname = $vals[$i]['tag'];
	                } else {
	                    $tagname = '';
	                }
	
	                if ( isset ( $children[$tagname] ) )
	                {
	                    $size = sizeof($children[$tagname]);
	                } else {
	                    $size = 0;
	                }
	
	                if ( isset ( $vals[$i]['attributes'] ) ) {
	                    $children[$tagname][$size]['@'] = $vals[$i]["attributes"];
	                }
	
	                $children[$tagname][$size]['#'] = $this->xml_depth($vals, $i);
	
	            break; 
	
	
	            case 'cdata':
	                array_push($children, $vals[$i]['value']); 
	            break; 
	
	            case 'complete': 
	                $tagname = $vals[$i]['tag'];
	
	                if( isset ($children[$tagname]) )
	                {
	                    $size = sizeof($children[$tagname]);
	                } else {
	                    $size = 0;
	                }
	
	                if( isset ( $vals[$i]['value'] ) )
	                {
	                    $children[$tagname][$size]["#"] = $vals[$i]['value'];
	                } else {
	                    $children[$tagname][$size]["#"] = '';
	                }
	
	                if ( isset ($vals[$i]['attributes']) ) {
	                    $children[$tagname][$size]['@']
	                                             = $vals[$i]['attributes'];
	                }			
	
	            break; 
	
	            case 'close':
	                return $children; 
	            break;
	        } 
	
	    } 
	
		return $children;
	
	}
	
	function traverse_xmlize($array, $arrName = "array", $level = 0) {
	
	    foreach($array as $key=>$val)
	    {
	        if ( is_array($val) )
	        {
	            traverse_xmlize($val, $arrName . "[" . $key . "]", $level + 1);
	        } else {
	            $GLOBALS['traverse_array'][] = '$' . $arrName . '[' . $key . '] = "' . $val . "\"\n";
	        }
	    }
	
	    return 1;
	
	}
}
?>