nodeName = $name; $this->attrs = $attrs; $this->childNodes = $children; $this->value = ""; } function getAttribute($at) { $val=$this->attrs[$at]; return $val; } function SSN($name,$test="") { $elch=&$this->childNodes; if ($elch) { $nelch=count($elch); for ($i=0;$i<$nelch;$i++) { $nd=$elch[$i]; if ($nd->nodeName==$name) { if ($test=="") return $nd; else { eval("\$condition=".$test); if ($condition) return $nd; } } } } return 0; } function SN($name,$test="") { $nodes=array(); $elch=&$this->childNodes; if ($elch) { $nelch=count($elch); $x=0; for ($i=0;$i<$nelch;$i++) { $nd=$elch[$i]; if ($nd->nodeName==$name) { if ($test=="") { $nodes[$x++]=$nd; } else { eval("\$condition=".$test); if ($condition) $nodes[$x++]=$nd; } } } } return $nodes; } // select single node function selectSingleNode($xpath) { $arrPath=split("/",$xpath); $larrPath=count($arrPath); $elem=array(); $cond=array(); for ($q=0;$q<$larrPath;$q++) { $str=preg_split("/[\[\]]/",$arrPath[$q]); $elem[$q]=$str[0]; $test=$str[1]; if ($test) { $test=preg_replace("/([^@])and/","$1&&",$test); $test=preg_replace("/([^@])or/","$1||",$test); $re="/(@([a-zA-Z0-9]+))/"; do { $oldtest=$test; $test=preg_replace($re,"\$nd->getAttribute('$2')",$oldtest); } while ($test!=$oldtest); $test=preg_replace("/@/","",$test); $test=preg_replace("/!=/","!@#",$test); $test=preg_replace("/=/","==",$test); $test=preg_replace("/!@#/","!=",$test); $cond[$q]=$test.";"; } else $cond[$q]=0; // echo ($elem[$q].": ".$cond[$q]."
"); } $nodes = array($this); for ($i=0;$i<$larrPath-1;$i++) { $nl=count($nodes); $nodes1 = array(); for ($n=0;$n<$nl;$n++) { $nodes2=$nodes[$n]->SN($elem[$i],$cond[$i]); $y=count($nodes1); for ($x=0;$xSSN($elem[$i],$cond[$i]); if ($node) return $node; } } function selectNodes($xpath) { $arrPath=split("/",$xpath); $larrPath=count($arrPath); $elem=array(); $cond=array(); for ($q=0;$q<$larrPath;$q++) { $str=preg_split("/[\[\]]/",$arrPath[$q]); $elem[$q]=$str[0]; $test=$str[1]; // echo ($elem[$q].": ".$test."
"); if ($test) { $test=preg_replace("/([^@])and/","$1&&",$test); $test=preg_replace("/([^@])or/","$1||",$test); $re="/(@([a-zA-Z0-9]+))/"; do { $oldtest=$test; $test=preg_replace($re,"\$nd->getAttribute('$2')",$oldtest); } while ($test!=$oldtest); $test=preg_replace("/@/","",$test); $test=preg_replace("/!=/","!@#",$test); $test=preg_replace("/=/","==",$test); $test=preg_replace("/!@#/","!=",$test); $cond[$q]=$test.";"; } else $cond[$q]=0; // echo ($elem[$q].": ".$cond[$q]."
"); } $nodes = array($this); for ($i=0;$i<$larrPath;$i++) { $nl=count($nodes); $nodes1 = array(); for ($n=0;$n<$nl;$n++) { $nodes2=$nodes[$n]->SN($elem[$i],$cond[$i]); $y=count($nodes1); for ($x=0;$xtag_tree=array(); $this->stack=array(); } function GetRoot() { return $this->stack[0]; } function SetXMLData( &$xml_data ) { $this->xml_parser = xml_parser_create(); xml_set_object( $this->xml_parser, &$this ); xml_set_element_handler($this->xml_parser, "_startTag", "_endTag"); xml_set_character_data_handler($this->xml_parser, "_characterData"); $data = xml_parse($this->xml_parser,$xml_data); if(!$data) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->xml_parser)), xml_get_current_line_number($this->xml_parser))); } xml_parser_free($this->xml_parser); } function _startTag($parser, $name, $attrs) { $tag = new tag($name,$attrs,''); array_push($this->stack,$tag); } function _characterData($parser, $data) { // eval($this->path." = '".trim($data)."';"); $cnt=count($this->stack); $this->stack[$cnt-1]->value.=trim($data); } function _endTag($parser, $name) { $cnt=count($this->stack); $this->stack[$cnt-2]->childNodes[] = $this->stack[$cnt-1]; array_pop($this->stack); } } function Debug($s) { print "\n
Debug: ".HTMLEntities($s)."
\n"; } $XML =''."\n"; $XML.=''."\n"; $XML.=' '."\n"; $XML.=' Adam'."\n"; $XML.=' Eve'."\n"; $XML.=' '."\n"; $XML.=' '."\n"; $XML.=' God'."\n"; $XML.=' '."\n"; $XML.=''."\n"; $XML.=''."\n"; $XML.=' '."\n"; $XML.=' Clinton'."\n"; $XML.=' Bush'."\n"; $XML.=' '."\n"; $XML.=' '."\n"; $XML.=' Tony'."\n"; $XML.=' Baggins'."\n"; $XML.=' '."\n"; $XML.=''."\n"; $XML.=''."\n"; Debug($XML); $xmldoc= & new XmlDom(); $xmldoc->SetXMLData($XML); $rt=$xmldoc->GetRoot(); //countries $countries=$rt->selectNodes("COUNTRY"); print("\n"); print("\n"); foreach ($countries as $country) { $cntrname=$country->getAttribute("NAME"); $cities=$country->selectNodes("CITY"); foreach ($cities as $city) { $cityname=$city->getAttribute("NAME"); $users=$city->selectNodes("USER"); foreach ($users as $user) { $username=$user->value; print("\n"); } } } print("
CountryCityUser
$cntrname$cityname$username
\n"); print("
\n"); print("Select users living in capital city:

\n"); $usersincapital=$rt->selectNodes("COUNTRY/CITY[@CAPITAL='1']/USER"); foreach ($usersincapital as $user) { $username=$user->value; print("$username
\n"); } print("
\n"); highlight_file ( "objxml.php"); ?>