PHP中的生成XML文件的4種方法分享
更新時間:2012年10月06日 12:44:58 作者:
PHP中的生成XML文件的4種方法分享,需要的朋友可以參考下
生成如下XML串
Xml代碼
<?xml version="1.0" encoding="utf-8"?>
<article>
<item>
<title size="1">title1</title>
<content>content1</content>
<pubdate>2009-10-11</pubdate>
</item>
<item>
<title size="1">title2</title>
<content>content2</content>
<pubdate>2009-11-11</pubdate>
</item>
</article>
方法I.【直接生成字符串】
使用純粹的PHP代碼生成字符串,并把這個字符串寫入一個以XML為后綴的文件。這是最原始的生成XML的方法,不過有效!
<?PHP
$data_array = array(
array(
'title' => 'title1',
'content' => 'content1',
'pubdate' => '2009-10-11',
),
array(
'title' => 'title2',
'content' => 'content2',
'pubdate' => '2009-11-11',
)
);
$title_size = 1;
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$xml .= "<article>\n";
foreach ($data_array as $data) {
$xml .= create_item($data['title'], $title_size, $data['content'], $data['pubdate']);
}
$xml .= "</article>\n";
echo $xml;
// 創(chuàng)建XML單項(xiàng)
function create_item($title_data, $title_size, $content_data, $pubdate_data)
{
$item = "<item>\n";
$item .= "<title size=\"" . $title_size . "\">" . $title_data . "</title>\n";
$item .= "<content>" . $content_data . "</content>\n";
$item .= " <pubdate>" . $pubdate_data . "</pubdate>\n";
$item .= "</item>\n";
return $item;
}
?>
方法2:【DomDocument】
使用DomDocument生成XML文件,創(chuàng)建節(jié)點(diǎn)使用createElement方法,創(chuàng)建文本內(nèi)容使用createTextNode方法,添加子節(jié)點(diǎn)使用appendChild方法,創(chuàng)建屬性使用createAttribute方法
<?php
$data_array = array(
array(
'title' => 'title1',
'content' => 'content1',
'pubdate' => '2009-10-11',
),
array(
'title' => 'title2',
'content' => 'content2',
'pubdate' => '2009-11-11',
)
);
// 屬性數(shù)組
$attribute_array = array(
'title' => array(
'size' => 1
)
);
// 創(chuàng)建一個XML文檔并設(shè)置XML版本和編碼。。
$dom=new DomDocument('1.0', 'utf-8');
// 創(chuàng)建根節(jié)點(diǎn)
$article = $dom->createElement('article');
$dom->appendchild($article);
foreach ($data_array as $data) {
$item = $dom->createElement('item');
$article->appendchild($item);
create_item($dom, $item, $data, $attribute_array);
}
echo $dom->saveXML();
function create_item($dom, $item, $data, $attribute) {
if (is_array($data)) {
foreach ($data as $key => $val) {
// 創(chuàng)建元素
$$key = $dom->createElement($key);
$item->appendchild($$key);
// 創(chuàng)建元素值
$text = $dom->createTextNode($val);
$$key->appendchild($text);
if (isset($attribute[$key])) {
// 如果此字段存在相關(guān)屬性需要設(shè)置
foreach ($attribute[$key] as $akey => $row) {
// 創(chuàng)建屬性節(jié)點(diǎn)
$$akey = $dom->createAttribute($akey);
$$key->appendchild($$akey);
// 創(chuàng)建屬性值節(jié)點(diǎn)
$aval = $dom->createTextNode($row);
$$akey->appendChild($aval);
}
} // end if
}
} // end if
} // end function
?>
方法3:【XMLWriter】
使用XMLWriter類創(chuàng)建XML文件,此方法在PHP 5.1.2后有效。另外,它可以輸出多種編碼的XML,但是輸入只能是utf-8
<?php
$data_array = array(
array(
'title' => 'title1',
'content' => 'content1',
'pubdate' => '2009-10-11',
),
array(
'title' => 'title2',
'content' => 'content2',
'pubdate' => '2009-11-11',
)
);
// 屬性數(shù)組
$attribute_array = array(
'title' => array(
'size' => 1
)
);
$xml = new XMLWriter();
$xml->openUri("php://output");
// 輸出方式,也可以設(shè)置為某個xml文件地址,直接輸出成文件
$xml->setIndentString(' ');
$xml->setIndent(true);
$xml->startDocument('1.0', 'utf-8');
// 開始創(chuàng)建文件
// 根結(jié)點(diǎn)
$xml->startElement('article');
foreach ($data_array as $data) {
$xml->startElement('item');
if (is_array($data)) {
foreach ($data as $key => $row) {
$xml->startElement($key);
if (isset($attribute_array[$key]) && is_array($attribute_array[$key]))
{
foreach ($attribute_array[$key] as $akey => $aval) {
// 設(shè)置屬性值
$xml->writeAttribute($akey, $aval);
}
}
$xml->text($row); // 設(shè)置內(nèi)容
$xml->endElement(); // $key
}
}
$xml->endElement(); // item
}
$xml->endElement(); // article
$xml->endDocument();
$xml->flush();
?>
方法4: 【SimpleXML】
使用SimpleXML創(chuàng)建XML文檔
<?php
$data_array = array(
array(
'title' => 'title1',
'content' => 'content1',
'pubdate' => '2009-10-11',
),
array(
'title' => 'title2',
'content' => 'content2',
'pubdate' => '2009-11-11',
)
);
// 屬性數(shù)組
$attribute_array = array(
'title' => array(
'size' => 1
)
);
$string = <<<XML
<?xml version='1.0' encoding='utf-8'?>
<article>
</article>
XML;
$xml = simplexml_load_string($string);
foreach ($data_array as $data) {
$item = $xml->addChild('item');
if (is_array($data)) {
foreach ($data as $key => $row) {
$node = $item->addChild($key, $row);
if (isset($attribute_array[$key]) && is_array($attribute_array[$key]))
{
foreach ($attribute_array[$key] as $akey => $aval) {
// 設(shè)置屬性值
$node->addAttribute($akey, $aval);
}
}
}
}
}
echo $xml->asXML();
?>
Xml代碼
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<article>
<item>
<title size="1">title1</title>
<content>content1</content>
<pubdate>2009-10-11</pubdate>
</item>
<item>
<title size="1">title2</title>
<content>content2</content>
<pubdate>2009-11-11</pubdate>
</item>
</article>
方法I.【直接生成字符串】
使用純粹的PHP代碼生成字符串,并把這個字符串寫入一個以XML為后綴的文件。這是最原始的生成XML的方法,不過有效!
復(fù)制代碼 代碼如下:
<?PHP
$data_array = array(
array(
'title' => 'title1',
'content' => 'content1',
'pubdate' => '2009-10-11',
),
array(
'title' => 'title2',
'content' => 'content2',
'pubdate' => '2009-11-11',
)
);
$title_size = 1;
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$xml .= "<article>\n";
foreach ($data_array as $data) {
$xml .= create_item($data['title'], $title_size, $data['content'], $data['pubdate']);
}
$xml .= "</article>\n";
echo $xml;
// 創(chuàng)建XML單項(xiàng)
function create_item($title_data, $title_size, $content_data, $pubdate_data)
{
$item = "<item>\n";
$item .= "<title size=\"" . $title_size . "\">" . $title_data . "</title>\n";
$item .= "<content>" . $content_data . "</content>\n";
$item .= " <pubdate>" . $pubdate_data . "</pubdate>\n";
$item .= "</item>\n";
return $item;
}
?>
方法2:【DomDocument】
使用DomDocument生成XML文件,創(chuàng)建節(jié)點(diǎn)使用createElement方法,創(chuàng)建文本內(nèi)容使用createTextNode方法,添加子節(jié)點(diǎn)使用appendChild方法,創(chuàng)建屬性使用createAttribute方法
復(fù)制代碼 代碼如下:
<?php
$data_array = array(
array(
'title' => 'title1',
'content' => 'content1',
'pubdate' => '2009-10-11',
),
array(
'title' => 'title2',
'content' => 'content2',
'pubdate' => '2009-11-11',
)
);
// 屬性數(shù)組
$attribute_array = array(
'title' => array(
'size' => 1
)
);
// 創(chuàng)建一個XML文檔并設(shè)置XML版本和編碼。。
$dom=new DomDocument('1.0', 'utf-8');
// 創(chuàng)建根節(jié)點(diǎn)
$article = $dom->createElement('article');
$dom->appendchild($article);
foreach ($data_array as $data) {
$item = $dom->createElement('item');
$article->appendchild($item);
create_item($dom, $item, $data, $attribute_array);
}
echo $dom->saveXML();
function create_item($dom, $item, $data, $attribute) {
if (is_array($data)) {
foreach ($data as $key => $val) {
// 創(chuàng)建元素
$$key = $dom->createElement($key);
$item->appendchild($$key);
// 創(chuàng)建元素值
$text = $dom->createTextNode($val);
$$key->appendchild($text);
if (isset($attribute[$key])) {
// 如果此字段存在相關(guān)屬性需要設(shè)置
foreach ($attribute[$key] as $akey => $row) {
// 創(chuàng)建屬性節(jié)點(diǎn)
$$akey = $dom->createAttribute($akey);
$$key->appendchild($$akey);
// 創(chuàng)建屬性值節(jié)點(diǎn)
$aval = $dom->createTextNode($row);
$$akey->appendChild($aval);
}
} // end if
}
} // end if
} // end function
?>
方法3:【XMLWriter】
使用XMLWriter類創(chuàng)建XML文件,此方法在PHP 5.1.2后有效。另外,它可以輸出多種編碼的XML,但是輸入只能是utf-8
復(fù)制代碼 代碼如下:
<?php
$data_array = array(
array(
'title' => 'title1',
'content' => 'content1',
'pubdate' => '2009-10-11',
),
array(
'title' => 'title2',
'content' => 'content2',
'pubdate' => '2009-11-11',
)
);
// 屬性數(shù)組
$attribute_array = array(
'title' => array(
'size' => 1
)
);
$xml = new XMLWriter();
$xml->openUri("php://output");
// 輸出方式,也可以設(shè)置為某個xml文件地址,直接輸出成文件
$xml->setIndentString(' ');
$xml->setIndent(true);
$xml->startDocument('1.0', 'utf-8');
// 開始創(chuàng)建文件
// 根結(jié)點(diǎn)
$xml->startElement('article');
foreach ($data_array as $data) {
$xml->startElement('item');
if (is_array($data)) {
foreach ($data as $key => $row) {
$xml->startElement($key);
if (isset($attribute_array[$key]) && is_array($attribute_array[$key]))
{
foreach ($attribute_array[$key] as $akey => $aval) {
// 設(shè)置屬性值
$xml->writeAttribute($akey, $aval);
}
}
$xml->text($row); // 設(shè)置內(nèi)容
$xml->endElement(); // $key
}
}
$xml->endElement(); // item
}
$xml->endElement(); // article
$xml->endDocument();
$xml->flush();
?>
方法4: 【SimpleXML】
使用SimpleXML創(chuàng)建XML文檔
復(fù)制代碼 代碼如下:
<?php
$data_array = array(
array(
'title' => 'title1',
'content' => 'content1',
'pubdate' => '2009-10-11',
),
array(
'title' => 'title2',
'content' => 'content2',
'pubdate' => '2009-11-11',
)
);
// 屬性數(shù)組
$attribute_array = array(
'title' => array(
'size' => 1
)
);
$string = <<<XML
<?xml version='1.0' encoding='utf-8'?>
<article>
</article>
XML;
$xml = simplexml_load_string($string);
foreach ($data_array as $data) {
$item = $xml->addChild('item');
if (is_array($data)) {
foreach ($data as $key => $row) {
$node = $item->addChild($key, $row);
if (isset($attribute_array[$key]) && is_array($attribute_array[$key]))
{
foreach ($attribute_array[$key] as $akey => $aval) {
// 設(shè)置屬性值
$node->addAttribute($akey, $aval);
}
}
}
}
}
echo $xml->asXML();
?>
您可能感興趣的文章:
相關(guān)文章
為Plesk PHP7啟用Oracle OCI8擴(kuò)展方法總結(jié)
在本篇文章里小編給大家總結(jié)了關(guān)于為Plesk PHP7啟用Oracle OCI8擴(kuò)展方法和相關(guān)代碼,需要的朋友們學(xué)習(xí)下。2019-03-03
PHP+JS實(shí)現(xiàn)文件分塊上傳的示例代碼
我們在上傳大文件時,可能會由于服務(wù)器的原因?qū)е挛募蟼魇?,文件過大時由于服務(wù)器的配置或響應(yīng)事件過長導(dǎo)致上傳文件失敗,這時候我們可以將一個大的文件分為若干塊,然后分批次上傳到服務(wù)端。本文介紹了實(shí)現(xiàn)的方法,需要的可以參考一下2022-11-11
PHP實(shí)現(xiàn)利用MySQL保存session的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)利用MySQL保存session的方法,是PHP程序設(shè)計中比較有實(shí)用價值的一個技巧,需要的朋友可以參考下2014-08-08
PHP面向?qū)ο蠓治鲈O(shè)計的61條軍規(guī)小結(jié)
你不必嚴(yán)格遵守這些原則,違背它們也不會被處以宗教刑罰。但你應(yīng)當(dāng)把這些原則看成警鈴,若違背了其中的一條,那么警鈴就會響起 。2010-07-07

