導航:首頁 > 編程語言 > php生成xml字元串

php生成xml字元串

發布時間:2022-08-27 10:04:35

php輸出xml內容

PHP生成XML的方法很多,這里演示最基本,最簡單的字元串構造法。就是使用字元串構造或者拼接成xml數據格式,然後輸出或者生成xml文件。

<?php
$data=array(
array(
'title'=>'',
'country'=>'china',
'name'=>'網路',
),
array(
'title'=>'google',
'country'=>'usa',
'name'=>'谷歌',
)
);

//構造xml數據格式
$xml="<?xmlversion="1.0"encoding="utf-8"?> ";
$xml.="<data> ";
foreach($dataas$itm){
//循環構造xml單項
$item="<item> ";
$item.="<title>".$itm['title']."</title> ";
$item.="<country>".$itm['country']."</country> ";
$item.="<name>".$itm['name']."</name> ";
$item.="</item> ";
$xml.=$item;
}

$xml.="</data> ";

//輸出xml數據
echo$xml;

?>

生成的數據格式如下:

Ⅱ 如何用PHP生成XML

$sql = "查詢資料庫文件";
$query = mysql_query($sql);
echo "<?xml version='1.0' encoding='utf-8' ?>";
echo "<photos>";
while(@$result = mysql_fetch_array($query)){

echo "<photo desc='$result[文件名欄位]' url='_pics/$result[文件名欄位]' />";

}
echo "</photos>";
--------------------------------------------------------
$this->_delImage('/_pics');

function _delImage($path){
if(is_dir($path)){
$dp=dir($path);
while($file=$dp->read())
if($file!='.'&&$file!='..'){
$this->_delImage($path.'/'.$file);
}
$dp->close();
}
echo "<photo desc='$path' url='$path' />";
}

可能有出入 自己看著修改

PS:我才昏呢 讀取資料庫比讀文件夾方便好不好。

Ⅲ php生成 xml 的問題

其實一般你動態生成網站最新的5萬條數據已經可以滿足要求了,少一點說最新的5000也是ok的,不過你真的想這么做的話
<?php
$i=0;
$fp_r = fopen('slnew.txt','r');//只讀模式打開txt文檔,數據源就算是資料庫也行,自己看著辦
$fp_w = fopen('islnew'.$i.'.txt','w');//只寫模式打開txt文檔
$content = '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
$content = $content.'<urlset>'."\r\n";
while(!feof($fp_r)){
if($i%50000==0){
fclose($fp_w);
$fp_w = fopen('islnew'.($i/50000).'.txt','w');//只寫模式打開txt文檔,這里已經在新建文件了
}
$get=fgets($fp_r);//讀取一行文字
$content = $content.'<url>'."\r\n";
$content = $content.'<loc>'.$get.'</loc>'."\r\n";
$content = $content.'</url>'."\r\n";
$i++;
}//輸出整個文本內容
$content = $content.'</urlset>'."\r\n";
fwrite($fp_w,$content);//寫入hello tocus!
fclose($fp_r);
fclose($fp_w);
?>
自己看著調整吧- -,我從以前寫過的內容里加了計數,你可以按照自己需求調整下

Ⅳ php頁面是gb2312編碼,想生成unicode的xml文件,該如何編寫php程序

如果你用的是php5的話,可能得看看
http://www.zypx.cn/technology/2010060619837242.html
php5和php6在unicode上的設定是有區別的,php6擴大了php5支持unicode的特性。
關於轉碼:
http://dodomail.javaeye.com/blog/195361
http://zhg.me/2009/07/php-unicode-transcoding-and-decoding-achieve.htm/comment-page-1?replytocom=472
轉碼時可能要安裝iconv:
http://apps.hi..com/share/detail/18328380
php幫助網站:
http://cn2.php.net/
關於文件內容寫入的例子:
http://cn2.php.net/fwrite

根據你的要求,如果要保存成unicode的xml文件的話可能麻煩點,看你的php是哪個版本的,有沒有iconv庫什麼的。
<?php
//gb2312轉換為unicode
function gb2un($g)//傳入gb2312字元串返回unicode碼
{
preg_match_all("/[\x80-\xff]?./",$g,$ar);
$str = "";
foreach($ar[0] as $v)
{
$str = $str."&#".utf8_unicode(iconv("gb2312","utf-8",$v)).";";
}
return $str;
}
$myString = gb2un("123我");
$fh=fopen('test.txt',"w");
fwrite($fh,unicode_encode($myString));
fclose($fh);
?>

Ⅳ 如何通過PHP生成和獲取XML格式數據

1自己拼,XML編碼

<?php
header('Content-type:text/xml');
echo "<?xml version='1.0' encoding='utf-8'>";
echo "<book>";
echo "<PHP>";
echo "<name>PHP程序開發範例寶典</name>";
echo "<price 單位='元/本'>89.00</price>";
echo "<date>2007-09-01</date>";
echo "</PHP>";
echo "</book>";
?>

拼接的效果

Ⅵ 如何在php文件里寫xml

php文件里寫xml方法:

1、Xml代碼


<?php
$data_array=array(
array(
'title'=>'title1',
'content'=>'content1',
'pubdate'=>'2009-10-11',
),
array(
'title'=>'title2',
'content'=>'content2',
'pubdate'=>'2009-11-11',
)
);
//屬性數組
$attribute_array=array(
'title'=>array(
'size'=>1
)
);
$string=<<<XML
<?xmlversion='1.0'encoding='utf-8'?>
<article>
</article>
XML;
$xml=simplexml_load_string($string);
foreach($data_arrayas$data){
$item=$xml->addChild('item');
if(is_array($data)){
foreach($dataas$key=>$row){
$node=$item->addChild($key,$row);
if(isset($attribute_array[$key])&&is_array($attribute_array[$key]))
{
foreach($attribute_array[$key]as$akey=>$aval){
//設置屬性值
$node->addAttribute($akey,$aval);
}
}
}
}
}
echo$xml->asXML();
?>

Ⅶ php把xml轉換為字元串

樓主我教你吧,首先xml文件里的內容為

<?xmlversion="1.0"encoding="ISO-8859-1"?>
<content
<name>lishi</name>
<age>17</age>
</content>

讀取xml文件內容

$str=file_get_contents($xml);$xml為xml文件路徑地址


將讀取的字元串內容轉化為xml對象

$obj=simplexml_load_string($str)


操作對象里的數據

$obj->name="lishi111";

$obj->age=77;


拼接新的字元串

$strNew="<?xml version='1.0' encoding='ISO-8859-1'?>";

$str.="<content><name>".$obj->name."</name";

$str.="<age>".$obj->age."</age></content>";


將新的字元串寫入xml文件

file_put_content($xml,$strNew);


最後一步拿分來吧,哈哈哈哈。有問題繼續。。。

Ⅷ php將XML轉換成字元串!

$str = $xml->asXML();

$str就是你要的字元串

Ⅸ php 查詢mysql生成xml

沒啥難的,只是你當它是xml罷了,把它看到字元串,拼成xml格式,再那麼一生成xml文件(或者直接用text/xml顯示)即可。

Ⅹ 如何用php從資料庫讀取數據並生成xml文件


我的思路是,直接使用動態的xml,讓flash讀取這個文檔,這樣就不用實時的去生成xml文件了。當然,這個xml文件是.php格式的,所以你必須在flash中吧讀取的文件地址改成php的,就跟你寫一個php頁面一樣,不同的是這個php文件輸出的內容是一個xml格式的文本。

比如你現在建立文件 xml.php
<?php
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<gallery>
<settings>";

//若此處也有動態信息 按需要進行調用

echo"</settings>
<items>";

//在此循環你的圖片數據
$data = ??
while( $data ) {
echo "<item source=\"".$data['source']."\" description=\"".$data['description']."\" />";
}

echo '</items>';
?>

閱讀全文

與php生成xml字元串相關的資料

熱點內容
編譯原理代碼在哪裡運行 瀏覽:584
解密攝影pdf 瀏覽:72
演算法編程中級題目 瀏覽:249
c語言編譯器畢業設計 瀏覽:715
醫保卡申請app哪個好 瀏覽:944
阿里雲伺服器上傳源碼 瀏覽:602
營銷管理科特勒pdf 瀏覽:696
願望清單app哪個好 瀏覽:459
安卓外放聲音怎麼解決 瀏覽:195
脈脈app干什麼用的 瀏覽:360
拽姐是哪個app 瀏覽:860
雲伺服器刪除了還有嗎 瀏覽:234
macbook可以用單片機嘛 瀏覽:309
南陽php招聘 瀏覽:816
去哪裡找按摩師很漂亮的app 瀏覽:821
86x99用簡便演算法計算 瀏覽:833
php截圖flash 瀏覽:276
卸載聯想app哪個好 瀏覽:722
php文字轉圖片 瀏覽:332
豆客後台怎麼加密碼 瀏覽:577