php文件生成xml文檔,php讀取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文件 高手進入 有加分

可以使用php里的dom,如下:

$xmlStr=[文件位置];

$dom = new DOMDocument(‘1.0’);

$dom -loadXML($xmlStr);

//根

$vogue=$dom-createElement(‘vogue’);

$dom-appendChild($vogue);

//第一級子目錄

$level0=$dom-createElement(‘level0’);

//第一級子目錄的屬性

$attr=$dom-createAttribute(‘parentTypeID’);

$attr-appendChild($dom-createTextNode(‘-1’));

……..

//保存

$dom-saveXML($xmlStr);

但是我更推薦使用print來列印後輸入文件。

因為dom的輸出無法分行,樣例如下:

?php

$books = array();

$books [] = array(

‘title’ = ‘PHP Hacks’,

‘author’ = ‘Jack Herrington’,

‘publisher’ = “O’Reilly”

);

$books [] = array(

‘title’ = ‘Podcasting Hacks’,

‘author’ = ‘Jack Herrington’,

‘publisher’ = “O’Reilly”

);

?

books

?php

foreach( $books as $book )

{

?

book

title?php echo( $book[‘title’] ); ?/title

author?php echo( $book[‘author’] ); ?

/author

publisher?php echo( $book[‘publisher’] ); ?

/publisher

/book

?php

}

?

/books

php讀取資料庫並生成xml文件

?php

//讀取資料庫我就不說了,從資料庫取出來就行了

#使用dom生成xml,注意生成的xml中會沒有空格。

$dom=new DOMDocument(‘1.0′,’utf-8’);

$time = time();

$path=”$time.xml”; // $path 為xml文件的存儲路徑。

$module=$dom-createElement(‘breakfast_menu’);//創建一個節點

$dom-appendChild($module); //在指定元素節點的最後一個子節點之後添加節點

$food=$dom-createElement(‘food’); //外body

$module-appendChild($food);

$name=$dom-createElement(‘name’); //內table

$name_value=$dom-createTextNode(‘測試數據1’);

$name-appendChild($name_value);

$food-appendChild($name);

$price=$dom-createElement(‘price’); //內table

$price_value=$dom-createTextNode(‘測試數據2’);

$price-appendChild($price_value);

$food-appendChild($price);

$description=$dom-createElement(‘description’); //內table

$description_value=$dom-createTextNode(‘測試數據3’);

$description-appendChild($description_value);

$food-appendChild($description);

$calories=$dom-createElement(‘calories’); //內table

$calories_value=$dom-createTextNode(‘測試數據4’);

$calories-appendChild($calories_value);

$food-appendChild($calories);

$dom-saveXML();

$dom-save($path);

//var_dump($dom-save($path));exit;

if($dom-saveXML()){

echo “生成成功:”.$dom-saveXML();

}else{

echo “生成失敗”;

}

?

php如何生成xml文件

?php

#使用dom生成xml,注意生成的xml中會沒有空格。

$dom=new DOMDocument(‘1.0′,’utf-8’);

$path=”test.xml”; // $path 為xml文件的存儲路徑。

$module=$dom-createElement(‘newmodule’);// root node

$dom-appendChild($module);

$year=$dom-createElement(‘year’); // add attribute node

$name=$dom-createAttribute(‘name’);

$name-nodeValue=”最新動態”;

$year-setAttributeNode($name);

$module-appendChild($year);

$news=$dom-createElement(‘news’);

$year-appendChild($news);

$date=$dom-createElement(‘date’);

$date_value=$dom-createTextNode(’01-24′);

$date-appendChild($date_value);

$news-appendChild($date);

$title=$dom-createElement(‘title’);

$title_value=$dom-createTextNode(‘![CDATA[最新動態]]’);

$title-appendChild($title_value);

$news-appendChild($title);

$info=$dom-createElement(‘info’);

$info_value=$dom-createTextNode(‘![CDATA[ 表面採用進口楸木木皮拼貼成精美的拼花,自然清晰的木材紋理得到完美的呈現,各種材質的合理搭配締造了雅意系列精緻的傢具產品。

]]’);

$info-appendChild($info_value);

$news-appendChild($info);

echo $dom-saveXML();

$dom-save($path);

?

php 生成xml高級

樓主啊,我看了你這段語句里沒有做任何可能影響xml結構的運算幹嘛非要用DomDocument來生成啊?

直接echo就完了,又簡單又明了。雖然有點土。但是土的東西就是專門用來對付簡單問題的。不過echo的時候要注意:

?xml version=”1.0″ encoding=”utf-8″?

之前不能有輸出。

還有之前的所有include都必須去掉?結尾符號,否則一些版本的php會在之前輸出一大段空格,導致xml頭無法被瀏覽器識別到。

php生成xml代碼

$docu=new DOMDocument(‘1.0′,’utf-8’);//聲明DOMDocument對象

$docu-formatOutput=true;//使用xml標準化格式輸出

$request=$docu-createElement(‘request’);//使用createElement創建一個request標籤

$action=$docu-createElement(‘action’);//在request標籤下創建action標籤

$value=$docu-createTextNode(‘regAndenter’);//在action里插入字元串

$action-appendChild($value);//

$request-appendChild($action);//將創建的action標籤添加到xml文件里

$xmlid=$docu-createElement(‘id’);

$value=$docu-createTextNode($dataid);

$xmlid-appendChild($value);

$request-appendChild($xmlid);

$phonenumber =$docu-createElement(‘phonenumber’);

$value=$docu-createTextNode($uname);

$phonenumber-appendChild($value);

$request-appendChild($phonenumber);

$password =$docu-createElement(‘password’);

$value=$docu-createTextNode($pass);

$password-appendChild($value);

$request-appendChild($password);

$email=$docu-createElement(’email’);

$value=$docu-createTextNode($emai);

$email-appendChild($value);

$request-appendChild($email);

$realname =$docu-createElement(‘realname’);

$value=$docu-createTextNode($name1);

$realname-appendChild($value);

$request-appendChild($realname);

$document_id =$docu-createElement(‘document_id’);

$value=$docu-createTextNode($idc);

$document_id-appendChild($value);

$request-appendChild($document_id);

$provincecode=$docu-createElement(‘provincecode’);

$value=$docu-createTextNode($privince);

$provincecode-appendChild($value);

$request-appendChild($provincecode);

$citycode =$docu-createElement(‘citycode’);

$value=$docu-createTextNode($city);

$citycode-appendChild($value);

$request-appendChild($citycode);

$grouptype =$docu-createElement(‘grouptype’);

$value=$docu-createTextNode($type);

$grouptype-appendChild($value);

$request-appendChild($grouptype);

$college =$docu-createElement(‘college’);

$value=$docu-createTextNode($colle);

$college-appendChild($value);

$request-appendChild($college);

$docu-appendChild($request);//將創建的request標籤添加到xml文件里

//$docu-save(‘wang.xml’);//生成xml文件

照著我這個改,希望你能看得懂

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/257412.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-15 12:45
下一篇 2024-12-15 12:45

相關推薦

發表回復

登錄後才能評論