導航:首頁 > 編程語言 > php寫入xml

php寫入xml

發布時間:2025-06-07 01:06:41

① 求幾個xml+php源碼,越簡單越好

兩個,第二個簡單一點
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

閱讀全文

與php寫入xml相關的資料

熱點內容
文件夾是只讀是什麼意思 瀏覽:85
伺服器如何知道訪問域名 瀏覽:316
java網路編程實驗總結 瀏覽:82
linux下dns伺服器配置 瀏覽:704
我的命令是絕對的 瀏覽:929
助飛器app在哪裡下 瀏覽:61
無廣告win10解壓縮 瀏覽:473
台灣的伺服器怎麼選雲伺服器 瀏覽:810
群暉媒體伺服器平板上怎麼看 瀏覽:622
pdf文件怎麼轉換成jpg格式 瀏覽:720
程序員訓練時間 瀏覽:657
書簽主頁源碼帶後台 瀏覽:570
神舟顯卡驅動解壓有兩個文件夾 瀏覽:362
接受調度命令的過程中 瀏覽:803
銀行下一代app在哪裡 瀏覽:423
加密學習資料及答案 瀏覽:334
我來貸app在哪裡下載 瀏覽:954
羽化命令使用 瀏覽:361
php提權木馬源碼 瀏覽:657
趣充app實名認證在哪裡 瀏覽:420