>
以下为引用的内容: <? // 目前实现了 Param, Map function LogText($str){ return $str; } class XStream { var $objName; var $objParam = array (); var $objMap ; ### 取数据 ### function readXMLParam($file){ try { $doc = &new DOMDocument(); $doc->load($file); $objNames = $doc->getElementsByTagName( $this->objName ); foreach ($objNames as $objName) { for($i=0;$i<count($this->objParam);$i++){ $returnParamsName = $this->objParam[$i]; $objParams = $objName->getElementsByTagName($returnParamsName); $returnParams [$returnParamsName] = $objParams->item(0)->nodeValue; } return $returnParams; } /* $doc = &new DOMDocument(); $doc->load($file); $agentAgentInfos = $doc->getElementsByTagName( "agent.AgentInfo" ); foreach ($agentAgentInfos as $agentAgentInfo) { $agentIds = $agentAgentInfo->getElementsByTagName( "agentId" ); $agentId = $agentIds->item(0)->nodeValue; echo $i."=I:".$agentId; echo " n"; $winCommissions = $agentAgentInfo->getElementsByTagName("winCommissions"); foreach($winCommissions as $winCommission){ $entrys = $winCommission->getElementsBYTagName("entry"); $j = 0; for($k=0;$k<$entrys->length;$k++){ $entry = $entrys->item($k); $strings = $entry->getElementsByTagName("string"); echo $j."=J:".$strings->item(0)->nodeValue; echo " n"; $j++; } } $i++; } */ }catch (Exception $e){ LogText($e->getMessage()); } } ### 取数据 ### function readXMLMap($file){ try { $doc = &new DOMDocument(); $doc->load($file); $objNames = $doc->getElementsByTagName( $this->objName ); $objMapArr = $this->objMap; foreach ($objNames as $objName) { ### objMap 存在 if(is_array($objMapArr)){ for(;$keyI=key($objMapArr);next($objMapArr)){ $returnObjMapName_i = $keyI; $objMaps = $objName->getElementsByTagName($keyI); foreach($objMaps as $objMap){ $entrys = $objMap->getElementsBYTagName("entry"); for($j=0;$j<$entrys->length;$j++){ $entry = $entrys->item($j); for($k=0;$k<count($objMapArr[$keyI][$j]);$k++){ $returnObjMapName_k = $objMapArr[$keyI][$j][$k]; $returnObjMapBodys = $entry->getElementsByTagName($returnObjMapName_k); $returnObjMaps[$returnObjMapName_i][$j][$returnObjMapName_k] = $returnObjMapBodys ->item(0)->nodeValue; } } } } } } return $returnObjMaps; }catch (Exception $e){ LogText($e->getMessage()); } } ### 写数据 ### } ####### TEST ####### class TestXStream{ function testReadXML(){ $xstream = &new XStream(); $file = "./test/getInfo.xml"; $objName = "agent.AgentInfo"; $xstream->objName = $objName; ### 测试Param ### $objParam = array("agentId","agentRole","credit"); $xstream->objParam= $objParam; print_r($xstream->readXMLParam($file)); ### 测试Map ### $objMap = array( "winCommissions" => array("0"=>array("string","big-decimal"),"1"=>array("string","big-decimal")), "loseCommissions" => array("0"=>array("string","big-decimal"),"1"=>array("string","big-decimal")), "shares" => array("0"=>array("string","big-decimal"),"1"=>array("string","big-decimal")) ); $xstream->objMap = $objMap ; print_r($xstream->readXMLMap($file)); } } ### Main() ### $testXStream = &new TestXStream(); $testXStream->testReadXML(); ?> |