phpxml數(shù)據(jù)庫代碼 php數(shù)據(jù)庫源碼

PHP生成和獲取XML格式數(shù)據(jù)

在做數(shù)據(jù)接口時(shí) 我們通常要獲取第三方數(shù)據(jù)接口或者給第三方提供數(shù)據(jù)接口 而這些數(shù)據(jù)格式通常是以XML或者JSON格式傳輸 本文將介紹如何使用PHP生成XML格式數(shù)據(jù)供第三方調(diào)用以及如何獲取第三方提供的XML數(shù)據(jù)

成都創(chuàng)新互聯(lián)公司從2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目做網(wǎng)站、網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元楊浦做網(wǎng)站,已為上家服務(wù),為楊浦各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108

生成XML格式數(shù)據(jù)

我們假設(shè)系統(tǒng)中有一張學(xué)生信息表student 需要提供給第三方調(diào)用 并有id name sex age分別記錄學(xué)生的姓名 性別 年齡等信息

CREATE TABLE `student` (

`id` int( ) NOT NULL auto_increment

`name` varchar( ) NOT NULL

`sex` varchar( ) NOT NULL

`age` *** allint( ) NOT NULL default

PRIMARY KEY? (`id`)

) ENGINE=MyISAM? DEFAULT CHARSET=utf ;

首先 建立createXML php文件 先連接數(shù)據(jù)庫 獲取數(shù)據(jù)

include_once ( connect php ) //連接數(shù)據(jù)庫

$sql = select * from student ;

$result = mysql_query($sql) or die( Invalid query: mysql_error())

while ($row = mysql_fetch_array($result)) {

$arr[] = array(

name = $row[ name ]

sex = $row[ sex ]

age = $row[ age ]

}

這個(gè)時(shí)候 數(shù)據(jù)就保存在$arr中 你可以使用print_r打印下數(shù)據(jù)測(cè)試

接著 建立xml 循環(huán)數(shù)組 將數(shù)據(jù)寫入到xml對(duì)應(yīng)的節(jié)點(diǎn)中

$doc = new DOMDocument( utf ) ? // 聲明版本和編碼

$doc formatOutput = true;

$r = $doc createElement( root )

$doc appendChild($r)

foreach ($arr as $dat) {

$b = $doc createElement( data )

$name = $doc createElement( name )

$name appendChild($doc createTextNode($dat[ name ]))

$b appendChild($name)

$sex = $doc createElement( sex )

$sex appendChild($doc createTextNode($dat[ sex ]))

$b appendChild($sex)

$age = $doc createElement( age )

$age appendChild($doc createTextNode($dat[ age ]))

$b appendChild($age)

$r appendChild($b)

}

echo $doc saveXML()

我們調(diào)用了PHP內(nèi)置的類DOMDocument來處理與生成xml 最終生成的xml格式請(qǐng)點(diǎn)擊這里看效果

?xml version= encoding= utf ?

root

data

name李王皓/name

sex男/sex

age /age

/data

/root

獲取XML格式數(shù)據(jù)

現(xiàn)在我們假設(shè)要從第三方獲取學(xué)生信息 數(shù)據(jù)格式是XML 我們需要使用PHP解析XML 然后將解析后的數(shù)據(jù)顯示或者寫入本地?cái)?shù)據(jù)庫 而這里關(guān)鍵的一步是解析XML

PHP有很多中方法可以解析XML 其中PHP提供了內(nèi)置的XMLReader類可以循序地瀏覽過xml檔案的節(jié)點(diǎn) 你可以想像成游標(biāo)走過整份文件的節(jié)點(diǎn) 并抓取需要的內(nèi)容 使用XMLReader是高效的 尤其是讀取非常大的xml數(shù)據(jù) 相對(duì)其他方法 使用XMLReader消耗內(nèi)存非常少

header( Content type:text/; Charset=utf )

$url = // helloweba /demo/importXML/createXML php ;

$reader = new XMLReader() ? //實(shí)例化XMLReader

$reader open($url) //獲取xml

$i= ;

while ($reader read()) {

if ($reader nodeType == XMLReader::TEXT) { //判斷node類型

$m = $i% ;

if($m== )

$name = $reader value;? //讀取node值

if($m== )

$sex = $reader value;

if($m== ){

$age = $reader value;

$arr[] = array(

name = $name

sex = $sex

age = $age

}

$i++;

}

}

//print_r($arr)

lishixinzhi/Article/program/PHP/201311/21636

php讀取數(shù)據(jù)庫并生成xml文件

?php

//讀取數(shù)據(jù)庫我就不說了,從數(shù)據(jù)庫取出來就行了

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

$dom=new DOMDocument('1.0','utf-8');

$time = time();

$path="$time.xml"; // $path 為xml文件的存儲(chǔ)路徑。

$module=$dom-createElement('breakfast_menu');//創(chuàng)建一個(gè)節(jié)點(diǎn)

$dom-appendChild($module); //在指定元素節(jié)點(diǎn)的最后一個(gè)子節(jié)點(diǎn)之后添加節(jié)點(diǎn)

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

$module-appendChild($food);

$name=$dom-createElement('name'); //內(nèi)table

$name_value=$dom-createTextNode('測(cè)試數(shù)據(jù)1');

$name-appendChild($name_value);

$food-appendChild($name);

$price=$dom-createElement('price'); //內(nèi)table

$price_value=$dom-createTextNode('測(cè)試數(shù)據(jù)2');

$price-appendChild($price_value);

$food-appendChild($price);

$description=$dom-createElement('description'); //內(nèi)table

$description_value=$dom-createTextNode('測(cè)試數(shù)據(jù)3');

$description-appendChild($description_value);

$food-appendChild($description);

$calories=$dom-createElement('calories'); //內(nèi)table

$calories_value=$dom-createTextNode('測(cè)試數(shù)據(jù)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代碼

$docu=new DOMDocument('1.0','utf-8');//聲明DOMDocument對(duì)象

$docu-formatOutput=true;//使用xml標(biāo)準(zhǔn)化格式輸出

$request=$docu-createElement('request');//使用createElement創(chuàng)建一個(gè)request標(biāo)簽

$action=$docu-createElement('action');//在request標(biāo)簽下創(chuàng)建action標(biāo)簽

$value=$docu-createTextNode('regAndenter');//在action里插入字符串

$action-appendChild($value);//

$request-appendChild($action);//將創(chuàng)建的action標(biāo)簽添加到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);//將創(chuàng)建的request標(biāo)簽添加到xml文件里

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

照著我這個(gè)改,希望你能看得懂

php生成xml代碼快說把

使用PHP DOMDocument創(chuàng)建動(dòng)態(tài)XML文件

當(dāng)處理基于XML應(yīng)用程序時(shí),開發(fā)者經(jīng)常需要建立XML編碼數(shù)據(jù)結(jié)構(gòu)。例如,Web中基于用戶輸入的XML狀態(tài)模板,服務(wù)器請(qǐng)求XML語句,以及基于運(yùn)行時(shí)間參數(shù)的客戶響應(yīng)。

盡管XML數(shù)據(jù)結(jié)構(gòu)的構(gòu)建比較費(fèi)時(shí),但如果使用成熟的PHP DOM應(yīng)用程序接口,一切都會(huì)變得簡(jiǎn)單明了。本文將向你介紹PHP DOM應(yīng)用程序接口的主要功能,演示如何生成一個(gè)正確的XML完整文件并將其保存到磁盤中。

創(chuàng)建文檔類型聲明

一般而言,XML聲明放在文檔頂部。在PHP中聲明十分簡(jiǎn)單:只需實(shí)例化一個(gè)DOM文檔類的對(duì)象并賦予它一個(gè)版本號(hào)。查看程序清單A:

程序清單 A

復(fù)制代碼 代碼如下:

?php

// create doctype

$dom = new DOMDocument("1.0");

// display document in browser as plain text

// display document in browser as plain text

// for readability purposes

header("Content-Type: text/plain");

// save and display tree

echo $dom-saveXML();

?

請(qǐng)注意DOM文檔對(duì)象的saveXML()方法。稍后我再詳細(xì)介紹這一方法,現(xiàn)在你只需要簡(jiǎn)單認(rèn)識(shí)到它用于輸出XML文檔的當(dāng)前快照到一個(gè)文件或?yàn)g覽器。在本例,為增強(qiáng)可讀性,我已經(jīng)將ASCII碼文本直接輸出至瀏覽器。在實(shí)際應(yīng)用中,可將以text/XML頭文件發(fā)送到瀏覽器。

如在瀏覽器中查看輸出,你可看到如下代碼:

?xml version="1.0"?

添加元素和文本節(jié)點(diǎn)

XML真正強(qiáng)大的功能是來自其元素與封裝的內(nèi)容。幸運(yùn)的是,一旦你初始化DOM文檔,很多操作變得很簡(jiǎn)單。此過程包含如下兩步驟:

對(duì)想添加的每一元素或文本節(jié)點(diǎn),通過元素名或文本內(nèi)容調(diào)用DOM文檔對(duì)象的createElement()或createTextNode()方法。這將創(chuàng)建對(duì)應(yīng)于元素或文本節(jié)點(diǎn)的新對(duì)象。

通過調(diào)用節(jié)點(diǎn)的appendChild()方法,并把其傳遞給上一步中創(chuàng)建的對(duì)象,并在XML文檔樹中將元素或文本節(jié)點(diǎn)添加到父節(jié)點(diǎn)。

以下范例將清楚地演示這2步驟,請(qǐng)查看程序清單B。

程序清單 B

復(fù)制代碼 代碼如下:

?php

// create doctype

$dom = new DOMDocument("1.0");

// display document in browser as plain text

// for readability purposes

header("Content-Type: text/plain");

// create root element

$root = $dom-createElement("toppings");

$dom-appendChild($root);

// create child element

$item = $dom-createElement("item");

$root-appendChild($item);

// create text node

$text = $dom-createTextNode("pepperoni");

$item-appendChild($text);

// save and display tree

echo $dom-saveXML();

?

這 里,我首先創(chuàng)建一個(gè)名字為toppings的根元素,并使它歸于XML頭文件中。然后,我建立名為item的元素并使它 歸于根元素。最后,我又創(chuàng)建一個(gè)值為“pepperoni”的文本節(jié)點(diǎn)并使它歸于item元素。最終結(jié)果如下:

復(fù)制代碼 代碼如下:

?xml version="1.0"?

toppings

itempepperoni/item

/toppings

如果你想添加另外一個(gè)topping,只需創(chuàng)建另外一個(gè)item并添加不同的內(nèi)容,如程序清單C所示。

程序清單C

復(fù)制代碼 代碼如下:

?php

// create doctype

$dom = new DOMDocument("1.0");

// display document in browser as plain text

// for readability purposes

header("Content-Type: text/plain");

// create root element

$root = $dom-createElement("toppings");

$dom-appendChild($root);

// create child element

$item = $dom-createElement("item");

$root-appendChild($item);

// create text node

$text = $dom-createTextNode("pepperoni");

$item-appendChild($text);

// create child element

$item = $dom-createElement("item");

$root-appendChild($item);

// create another text node

$text = $dom-createTextNode("tomato");

$item-appendChild($text);

// save and display tree

echo $dom-saveXML();

?

以下是執(zhí)行程序清單C后的輸出:

復(fù)制代碼 代碼如下:

?xml version="1.0"?

toppings

itempepperoni/item

itemtomato/item

/toppings

添加屬性

通過使用屬性,你也可以添加適合的信息到元素。對(duì)于PHP DOM API,添加屬性需要兩步:首先用DOM文檔對(duì)象的createAttribute()方法創(chuàng)建擁有此屬性名字的節(jié)點(diǎn),然后將文檔節(jié)點(diǎn)添加到擁有屬性值的屬性節(jié)點(diǎn)。詳見程序清單D。

程序清單 D

復(fù)制代碼 代碼如下:

?php

// create doctype

$dom = new DOMDocument("1.0");

// display document in browser as plain text

// for readability purposes

header("Content-Type: text/plain");

// create root element

$root = $dom-createElement("toppings");

$dom-appendChild($root);

// create child element

$item = $dom-createElement("item");

$root-appendChild($item);

// create text node

$text = $dom-createTextNode("pepperoni");

$item-appendChild($text);

// create attribute node

$price = $dom-createAttribute("price");

$item-appendChild($price);

// create attribute value node

$priceValue = $dom-createTextNode("4");

$price-appendChild($priceValue);

// save and display tree

echo $dom-saveXML();

?

輸出如下所示:

復(fù)制代碼 代碼如下:

?xml version="1.0"?

toppings

item price="4"pepperoni/item

/toppings

添加CDATA模塊和過程向?qū)?/p>

雖然不經(jīng)常使用CDATA模塊和過程向?qū)В峭ㄟ^調(diào)用DOM文檔對(duì)象的createCDATASection()和createProcessingInstruction()方法, PHP API 也能很好地支持CDATA和過程向?qū)?,?qǐng)見程序清單E。

程序清單 E

復(fù)制代碼 代碼如下:

?php

// create doctype

// create doctype

$dom = new DOMDocument("1.0");

// display document in browser as plain text

// for readability purposes

header("Content-Type: text/plain");

// create root element

$root = $dom-createElement("toppings");

$dom-appendChild($root);

// create child element

$item = $dom-createElement("item");

$root-appendChild($item);

// create text node

$text = $dom-createTextNode("pepperoni");

$item-appendChild($text);

// create attribute node

$price = $dom-createAttribute("price");

$item-appendChild($price);

// create attribute value node

$priceValue = $dom-createTextNode("4");

$price-appendChild($priceValue);

// create CDATA section

$cdata = $dom-createCDATASection(" Customer requests that pizza be sliced into 16 square pieces ");

$root-appendChild($cdata);

// create PI

$pi = $dom-createProcessingInstruction("pizza", "bake()");

$root-appendChild($pi);

// save and display tree

echo $dom-saveXML();

?

輸出如下所示:

復(fù)制代碼 代碼如下:

?xml version="1.0"?

toppings

item price="4"pepperoni/item

![CDATA[

Customer requests that pizza be sliced into 16 square pieces

]]

?pizza bake()?

/toppings

保存結(jié)果

一旦已經(jīng)實(shí)現(xiàn)你的目標(biāo),就可以將結(jié)果保存在一個(gè)文件或存儲(chǔ)于PHP的變量。通過調(diào)用帶有文件名的save()方法可以將結(jié)果保存在文件中,而通過調(diào)用saveXML()方法可存儲(chǔ)于PHP的變量。請(qǐng)參考以下實(shí)例(程序清單F):

程序清單 F

復(fù)制代碼 代碼如下:

?php

// create doctype

$dom = new DOMDocument("1.0");

// create root element

$root = $dom-createElement("toppings");

$dom-appendChild($root);

$dom-formatOutput=true;

// create child element

$item = $dom-createElement("item");

$root-appendChild($item);

// create text node

$text = $dom-createTextNode("pepperoni");

$item-appendChild($text);

// create attribute node

$price = $dom-createAttribute("price");

$item-appendChild($price);

// create attribute value node

$priceValue = $dom-createTextNode("4");

$price-appendChild($priceValue);

// create CDATA section

$cdata = $dom-createCDATASection(" Customer requests that pizza be

sliced into 16 square pieces ");

$root-appendChild($cdata);

// create PI

$pi = $dom-createProcessingInstruction("pizza", "bake()");

$root-appendChild($pi);

// save tree to file

$dom-save("order.xml");

// save tree to string

$order = $dom-save("order.xml");

?

下面是實(shí)際的例子,大家可以測(cè)試下。

xml.php(生成xml)

復(fù)制代碼 代碼如下:

?

$conn = mysql_connect('localhost', 'root', '123456') or die('Could not connect: ' . mysql_error());

mysql_select_db('vdigital', $conn) or die ('Can\'t use database : ' . mysql_error());

$str = "SELECT id,username FROM `admin` GROUP BY `id` ORDER BY `id` ASC";

$result = mysql_query($str) or die("Invalid query: " . mysql_error());

if($result)

{

$xmlDoc = new DOMDocument();

if(!file_exists("01.xml")){

$xmlstr = "?xml version='1.0' encoding='utf-8' ?message/message";

$xmlDoc-loadXML($xmlstr);

$xmlDoc-save("01.xml");

}

else { $xmlDoc-load("01.xml");}

$Root = $xmlDoc-documentElement;

while ($arr = mysql_fetch_array($result)){

$node1 = $xmlDoc-createElement("id");

$text = $xmlDoc-createTextNode(iconv("GB2312","UTF-8",$arr["id"]));

$node1-appendChild($text);

$node2 = $xmlDoc-createElement("name");

$text2 = $xmlDoc-createTextNode(iconv("GB2312","UTF-8",$arr["username"]));

$node2-appendChild($text2);

$Root-appendChild($node1);

$Root-appendChild($node2);

$xmlDoc-save("01.xml");

}

}

mysql_close($conn);

?

test.php(應(yīng)用測(cè)試)

復(fù)制代碼 代碼如下:

?

$xmlDoc = new DOMDocument();

$xmlDoc-load("");

$x=$xmlDoc-getElementsByTagName('name');

for ($i=0; $i=$x-length-1; $i++)

{

if(strpos($x-item($i)-nodeValue,"fang")!==false)

{

echo $x-item($i)-parentNode-childNodes-item(1)-nodeValue;

}

}

?

網(wǎng)頁名稱:phpxml數(shù)據(jù)庫代碼 php數(shù)據(jù)庫源碼
網(wǎng)頁鏈接:http://muchs.cn/article4/ddcccie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、定制開發(fā)網(wǎng)站營銷、商城網(wǎng)站Google搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司