function GetXMLTree($data) { // $data = implode('', file($file)); // by: waldo@wh-e.com - trim space around tags not within //$data = eregi_replace(">"."[[:space:]]+"."<","><",$data); $data = preg_replace('/>\s+</', '><', $data); // XML functions $p = xml_parser_create(); // by: anony@mous.com - meets XML 1.0 specification xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0); xml_parse_into_struct($p, $data, &$vals, &$index); xml_parser_free($p); $i = 0; $tree = array(); $tree[] = array( 'tag' => $vals[$i]['tag'], 'attributes' => $vals[$i]['attributes'], 'value' => $vals[$i]['value'], 'children' => GetChildren($vals, $i) ); //debmes('xmltree - step 6'); return $tree; }
function GetXMLTree($data, $uppercase = 1) { $p = xml_parser_create(); xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1); xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, $uppercase); xml_parse_into_struct($p, $data, $vals, $index); xml_parser_free($p); $tree = array(); $i = 0; $tree = GetChildren($vals, $i); return $tree; }
function GetXMLTree($xmlloc) { if (file_exists($xmlloc)) { $data = implode('', file($xmlloc)); } else { $fp = fopen($xmlloc, 'r'); $data = fread($fp, 100000000); fclose($fp); } $parser = xml_parser_create('ISO-8859-1'); xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); xml_parse_into_struct($parser, $data, $vals, $index); xml_parser_free($parser); $tree = array(); $i = 0; if (isset($vals[$i]['attributes'])) { $tree[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes']; $index = count($tree[$vals[$i]['tag']]) - 1; $tree[$vals[$i]['tag']][$index] = array_merge($tree[$vals[$i]['tag']][$index], GetChildren($vals, $i)); } else { $tree[$vals[$i]['tag']][] = GetChildren($vals, $i); } return $tree; }
function xmlParse($data, $bList = "") { $bArray = array(); // if any attributes were passed to the function, add them to the array if (strlen($bList) > 0) { $bArray = explode(",", $bList); } // by: waldo@wh-e.com - trim space around tags not within $data = eregi_replace(">" . "[[:space:]]+" . "<", "><", $data); // XML functions $p = xml_parser_create(); // by: anony@mous.com - meets XML 1.0 specification xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0); xml_parse_into_struct($p, $data, $vals, $index); xml_parser_free($p); for ($x = 0; $x < count($vals); $x++) { if (array_key_exists("attributes", $vals[$x])) { foreach ($vals[$x]["attributes"] as $thiskey => $thisvalue) { // if the attribute name exists in the "bList" then re-cast the string to a boolean if (is_string($thisvalue) && array_search($thiskey, $bArray) !== false && (strtolower($thisvalue) == "true" || strtolower($thisvalue) == "false")) { $vals[$x]["attributes"][$thiskey] = strtolower($thisvalue) == "true"; } } } } $i = 0; $tree["xmlChildren"] = array(); $tree["xmlChildren"][] = array('xmlName' => $vals[$i]['tag'], 'xmlAttributes' => getAttributes($vals, $i), 'xmlValue' => getValue($vals, $i), 'xmlChildren' => GetChildren($vals, $i)); return $tree; }
function GetChildren($vals, &$i, $type) { if ($type == 'complete') { if (isset($vals[$i]['value'])) { return $vals[$i]['value']; } else { return ''; } } $children = array(); // Contains node data /* Loop through children */ while ($vals[++$i]['type'] != 'close') { $type = $vals[$i]['type']; // first check if we already have one and need to create an array if (isset($children[$vals[$i]['tag']])) { if (is_array($children[$vals[$i]['tag']])) { $temp = array_keys($children[$vals[$i]['tag']]); // there is one of these things already and it is itself an array if (is_string($temp[0])) { $a = $children[$vals[$i]['tag']]; unset($children[$vals[$i]['tag']]); $children[$vals[$i]['tag']][0] = $a; } } else { $a = $children[$vals[$i]['tag']]; unset($children[$vals[$i]['tag']]); $children[$vals[$i]['tag']][0] = $a; } $children[$vals[$i]['tag']][] = GetChildren($vals, $i, $type); } else { $children[$vals[$i]['tag']] = GetChildren($vals, $i, $type); } // I don't think I need attributes but this is how I would do them: if (isset($vals[$i]['attributes'])) { $attributes = array(); foreach (array_keys($vals[$i]['attributes']) as $attkey) { $attributes[$attkey] = $vals[$i]['attributes'][$attkey]; } // now check: do we already have an array or a value? if (isset($children[$vals[$i]['tag']])) { // case where there is an attribute but no value, a complete with an attribute in other words if ($children[$vals[$i]['tag']] == '') { unset($children[$vals[$i]['tag']]); $children[$vals[$i]['tag']] = $attributes; } elseif (is_array($children[$vals[$i]['tag']])) { $index = count($children[$vals[$i]['tag']]) - 1; // probably also have to check here whether the individual item is also an array or not or what... all a bit messy if ($children[$vals[$i]['tag']][$index] == '') { unset($children[$vals[$i]['tag']][$index]); $children[$vals[$i]['tag']][$index] = $attributes; } $children[$vals[$i]['tag']][$index] = array_merge($children[$vals[$i]['tag']][$index], $attributes); } else { $value = $children[$vals[$i]['tag']]; unset($children[$vals[$i]['tag']]); $children[$vals[$i]['tag']]['value'] = $value; $children[$vals[$i]['tag']] = array_merge($children[$vals[$i]['tag']], $attributes); } } else { $children[$vals[$i]['tag']] = $attributes; } } } return $children; }
/** * Get an XML tree * * Create an XML tree in array form from a large string. * * @uses GetChildren() * * @param string $data Large string that needs to be turned into XML data. * * @return array Multi dimensional array in an XML way containing XML data. */ function GetXMLTree($data) { $p = xml_parser_create(); xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0); xml_parse_into_struct($p, $data, $vals, $index); xml_parser_free($p); $tree = array(); $i = 0; if (!isset($vals[$i]['attributes'])) { $vals[$i]['attributes'] = ""; } array_push($tree, array('tag' => $vals[$i]['tag'], 'attributes' => $vals[$i]['attributes'], 'children' => GetChildren($vals, $i))); return $tree; }