Zend - The PHP Company




XML

Add Code


XML @=@ Associative Array  

Type: class library
Added by: nickc1998
Entered: 03/08/2002
Last modified: 08/12/2001
Rating: **** (6 votes)
Views: 14338
Allows XML to easily be parsed into an Associative Array with many options. It also allows Associative Arrays (no limit on levels) to be converted to XML.


<?php
/*

  XML Methods
  --------------------------------------------------------------
    XML_Array
      Usage: $xml = new XML_Array(xmldata(string),ignorefields(array 1,2,3),replacefields(array OLD => NEW),show attributes?,put attribs in seperate array)
         $array = $xml->Return();

    Array_XML
      Usage: $array = new Array_XML()
         $xml = $array->Return();
  --------------------------------------------------------------
*/

    
class Array_XML {
    var 
$_data;
    var 
$_name = Array();
    var 
$_rep  = Array();
    var 
$_parser 0;
    var 
$_ignore,$_err,$_errline,$_replace,$_attribs,$_parent;
    var 
$_level 0;

    function 
Array_XML(&$data,$replace = Array(),$attribs = Array()) {
        
$this->_attribs $attribs;
        
$this->_replace $replace;
        
$this->_data $this->_processArray($data);
    }
    function & 
ReturnXML() {
        return 
$this->_data;
    }
    function 
_processArray(&$array,$level 0) {
        
//ksort($array);
        
foreach ($array as $name => $value) {
        
$tlevel $level;
        
$isarray false;
        
$attrs '';

        if (
is_array($value) && sizeof($value) > && array_key_exists(0,$value)) { $tlevel $level 1$isarray true; }
        elseif (!
is_int($name)) $this->_rep[$name]++;
        else { 
$name $this->_parent$this->_rep[$name]++; }

        if (
is_array($this->_attribs[$tlevel][$name][$this->_rep[$name]-1])) {
            foreach (
$this->_attribs[$tlevel][$name][$this->_rep[$name]-1] as $aname => $avalue) {
            unset(
$value[$aname]);
            
$attrs .= $aname="$avalue"";
            }
        }
        if (
$this->_replace[$name]) $name $this->_replace[$name];

        
$this->_parent $name;
        
is_array($value) ? $output $this->_processArray($value,$tlevel+1) : $output htmlspecialchars($value);
        
$isarray ?
            
$return .= $output :
            
$return .= "<$name$attrs>$output</$name>n";
        }
        return 
$return;
    }
    }

    class 
XML_Array {
    var 
$_data = Array();
    var 
$_name = Array();
    var 
$_rep  = Array();
    var 
$_parser 0;
    var 
$_ignore = Array(),$_replace = Array(),$_showAttribs;
    var 
$_level 0;

    function 
XML_Array(&$data,$ignore = Array(),$replace = Array(),$showattribs false,$toupper false) {
        
$this->_showAttribs $showattribs;
        
$this->_parser  xml_parser_create();

        
xml_set_object($this->_parser,$this);
        if (
$toupper) {
        foreach (
$ignore  as $key => $value$this->_ignore[strtoupper($key)]  = strtoupper($value);
        foreach (
$replace as $key => $value$this->_replace[strtoupper($key)] = strtoupper($value);
        
xml_parser_set_option($this->_parser,XML_OPTION_CASE_FOLDING,true);
        }
        else {
        
$this->_ignore  = &$ignore;
        
$this->_replace = &$replace;
        
xml_parser_set_option($this->_parser,XML_OPTION_CASE_FOLDING,false);
        }
        
xml_set_element_handler($this->_parser"_startElement""_endElement");
        
xml_set_character_data_handler($this->_parser"_cdata");

        
$this->_data = array();
        
$this->_level 0;
        if(!
xml_parse($this->_parser$datatrue)){
        
//new Error("XML Parse Error: ".xml_error_string(xml_get_error_code($this->_parser))."n on line: ".xml_get_current_line_number($this->_parser),true);
        
return false;
        }
        
xml_parser_free($this->_parser);
    }
    function & 
ReturnArray() {
        return 
$this->_data[0];
    }
    function & 
ReturnReplaced() {
        return 
$this->_data['_Replaced_'];
    }
    function & 
ReturnAttributes() {
        return 
$this->_data['_Attributes_'];
    }
    function 
_startElement($parser$name$attrs){
        if (!isset(
$this->_rep[$name])) $this->_rep[$name] = 0;
        if (!
in_array($name,$this->_ignore)) {
        
$this->_addElement($name,$this->_data[$this->_level],$attrs,true);
        
$this->_name[$this->_level] = $name;
        
$this->_level++;
        }
    }
    function 
_endElement($parser,$name){
        if (!
in_array($name,$this->_ignore) && isset($this->_name[$this->_level 1])) {
        if (isset(
$this->_data[$this->_level])){
            
$this->_addElement($this->_name[$this->_level 1],$this->_data[$this->_level 1],$this->_data[$this->_level],false);
        }
        unset(
$this->_data[$this->_level]);
        
$this->_level--;
        
$this->_rep[$name]++;
        }
    }
    function 
_cdata($parser$data) {
        if (
$this->_name[$this->_level 1]) {
        
$this->_addElement($this->_name[$this->_level 1],$this->_data[$this->_level 1],str_replace(array("&gt;""&lt;","&quot;""&amp;"), array(">","<",'"',"&"),$data),false);
        }
    }
    function 
_addElement(&$name,&$start,$add = array(),$isattribs false) {
        if ((
sizeof($add) == && is_array($add)) || !$add) {
        if (!isset(
$start[$name])) $start[$name] = '';
        
$add '';
        
//if (is_array($add)) return;
        //return;
        
}
        if (
strtoupper($this->_replace[$name]) == '_ARRAY_') {
        if (!
$start[$name]) $this->_rep[$name] = 0;
        
$update = &$start[$name][$this->_rep[$name]];
        }
        elseif (
$this->_replace[$name]) {
        if (
$add[$this->_replace[$name]]) { $this->_data['_Replaced_'][$add[$this->_replace[$name]]] = $name$name $add[$this->_replace[$name]]; }
        
$update = &$start[$name];
        }
        else {
        
$update = &$start[$name];
        }

        if     (
$isattribs && !$this->_showAttribs) return;
        elseif (
$isattribs$this->_data['_Attributes_'][$this->_level][$name][] = $add;
        elseif (
is_array($add) && is_array($update)) $update += $add;
        elseif (
is_array($update)) return;
        elseif (
is_array($add)) $update $add;
        elseif (
$add)   $update .= $add;
    }
    }
?>


Usage Example


    XML_Array -- Convert XML to Associative Array
      Usage:   $xml = new XML_Array(xmldata(string),ignorefields(array 1,2,3),replacefields(array OLD => NEW),show attributes?,convert to upper)
           $array = $xml->ReturnArray();
      Example: $xml = new XML_Array('http://www.slashdot.org/slashdot.xml',array('backslash'),array('story' => '_array_'),true,false);
           print_r($xml->ReturnArray());
           print_r($xml->ReturnReplaced());
           print_r($xml->ReturnAttributes());

    Array_XML -- Convert Associative Array to XML
      Usage: $array = new Array_XML($xml->ReturnArray(),$xml->ReturnReplaced(),$xml->ReturnAttributes())
         $xml = $array->Return();


Rate This Script





Search



This Category All Categories