两个,第二个简单一点
1、xml+php动态载入与分页
//$this->iGlobal($vars,$cookie);
$this->outPut($vars,$cookie);
}
//function _shoplist_res(){
// $this->db->Close();
//}
function outPut($vars,$cookie){
/*$strQuery = "select count(*) as rc from ".$this->db_c->Db_Pre."pshop order by id desc";
$this->db->Query($strQuery);
$RecordCount = $this->db->Assoc();
$RecordCount = $RecordCount['rc'];
@$this->db->freeResult();*/
$RecordCount = 25;
if (empty($vars['pageNo'])) $vars['pageNo']= 1;
if (empty($vars['pageSize'])) $vars['pageSize']= 10;
$PageCount = ceil($RecordCount/$vars['pageSize']);
if ($vars['pageSize']>$RecordCount) $vars['pageSize']=$RecordCount;
if ($vars['pageNo']>$PageCount) $vars['pageNo']=$PageCount;
$xmlStr.= "<?xml version=\"1.0\" encoding=\"GBK\" ?>"."\n";
$xmlStr.= "<商铺列表>"."\n";
$xmlStr.= "\t<翻页工具 pageNo=\"".$vars['pageNo']."\" pageSize=\"".$vars['pageSize']."\" total=\"".$RecordCount."\"></翻页工具>"."\n";
$xmlStr.= "\t<页号码>".$vars['pageNo']."</页号码>"."\n";
$xmlStr.= "\t<页大小>".$vars['pageSize']."</页大小>"."\n";
$xmlStr.= "\t<数量>".$RecordCount."</数量>"."\n";
$xmlStr.= "\t<页数量>".$PageCount."</页数量>"."\n";
//$strQuery = "select t1.*,t2.* from ".$this->db_c->Db_Pre."pshop as t1,".$this->db_c->Db_Pre."shoptype as t2 where t1.typenum=t2.typenum order by id desc "
// ."limit ".($vars['pageNo']-1)*$vars['pageSize'].",".$vars['pageSize'];
//die($strQuery);
//$this->db->Query($strQuery);
/*while($result = $this->db->Assoc()){
$xmlStr.= "\t"."\n";
$xmlStr.= "\t<商铺>"."\n";
$xmlStr.= "\t\t<名称><![CDATA[".$result['shopname']."]]></名称>"."\n";
$xmlStr.= "\t\t<经营人><![CDATA[".$result['shopmaster']."]]></经营人>"."\n";
$xmlStr.= "\t\t<级别><![CDATA[".$result['shoptype']."]]></级别>"."\n";
$xmlStr.= "\t\t<简介><![CDATA[".$result['shopintro']."]]></简介>"."\n";
$xmlStr.= "\t\t<连接地址><![CDATA[personal_shop.php?id=".$result['id']."]]></连接地址>"."\n";
$xmlStr.= "\t</商铺>"."\n";
}*/
$sc= $vars['pageNo']*$vars['pageSize'];
if ($sc>$RecordCount) $sc = $RecordCount;
for($i=($vars['pageNo']-1)*$vars['pageSize'];$i<$sc;$i++){
$xmlStr.= "\t"."\n";
$xmlStr.= "\t<商铺>"."\n";
$xmlStr.= "\t\t<名称><![CDATA[这是名称:".$i."]]></名称>"."\n";
$xmlStr.= "\t\t<经营人><![CDATA[这是经营人:".$i."]]></经营人>"."\n";
$xmlStr.= "\t\t<级别><![CDATA[这是级别:".$i."]]></级别>"."\n";
$xmlStr.= "\t\t<简介><![CDATA[这是简介:".$i."]]></简介>"."\n";
$xmlStr.= "\t\t<连接地址><![CDATA[personal_shop.php?id=".$i."]]></连接地址>"."\n";
$xmlStr.= "\t</商铺>"."\n";
}
$xmlStr.= "</商铺列表>"."\n";
header("Content-type: text/xml\n\n");
echo $xmlStr;
}
}
$shoplist = new shoplist_res($vars,$HTTP_COOKIE_VARS);
//$shoplist->_shoplist_res();
unset($sthoplist);
?>
2、用php读取xml数据
<?php
$parser = xml_parser_create(); //创建一个parser编辑器
xml_set_element_handler($parser, "startElement", "endElement");//设立标签触发时的相应函数 这里分别为startElement和endElenment
xml_set_character_data_handler($parser, "characterData");//设立数据读取时的相应函数
$xml_file="1.xml";//指定所要读取的xml文件,可以是url
$filehandler = fopen($xml_file, "r");//打开文件
while ($data = fread($filehandler, 4096))
{
xml_parse($parser, $data, feof($filehandler));
}//每次取出4096个字节进行处理
fclose($filehandler);
xml_parser_free($parser);//关闭和释放parser解析器
$name=false;
$position=false;
function startElement($parser_instance, $element_name, $attrs) //起始标签事件的函数
{
global $name,$position;
if($element_name=="NAME")
{
$name=true;
$position=false;
echo "名字:";
}
if($element_name=="POSITION")
{$name=false;
$position=true;
echo "职位:";
}
}
function characterData($parser_instance, $xml_data) //读取数据时的函数
{
global $name,$position;
if($position)
echo $xml_data."<br>";
if($name)
echo $xml_data."<br>";
}
function endElement($parser_instance, $element_name) //结束标签事件的函数
{
global $name,$position;
$name=false;
$position=false;
}
?>
xml文件代码如下:
<?xml version="1.0"?>
<employees>
<employee>
<name>张三</name>
<position age="45">经理</position>
</employee>
<employees>
<employee>
<name>李四</name>
<position age="45">助理</position>
</employee>
</employees>
② PHP读取xml文件
以前做过联通或电信的service订购接口,收到的内容就是XML的,需要用到PHP的XML处理功能,你的代码可以这样:
<xmp>
<?php
$string=file_get_contents("http://www.orderjiaju.com/zixun/data/rss/3.xml");
if($string!=""):
$xml = new DomDocument('1.0');
$xml->loadXML($string);
//班台
$BanTai=array(
'Title'=>$xml->getElementsByTagName('channel')->item(0)->childNodes->item(1)->nodeValue,
'Link' =>$xml->getElementsByTagName('channel')->item(0)->childNodes->item(3)->nodeValue
);
for($i=0;$i<3;$i++)
{
$Title[]=array(
'Title'=>$xml->getElementsByTagName('item')->item($i)->childNodes->item(1)->nodeValue,
'Link' =>$xml->getElementsByTagName('item')->item($i)->childNodes->item(3)->nodeValue
);
}
print_r($BanTai);
print_r($Title);
endif;
?>
</xmp>
回答补充:
现在你直接,然后执行没效果吗?
我这里是可以的呢,测试网址是:service.020i.net/test_xml.php