PHP讀寫XML檔案技巧

來源:互聯網
上載者:User

 常用 如下 幾行:

header("content-type:text/html; charset=utf-8"); //指定PHP使用UTF-8編碼
$xml = simplexml_load_file("example.xml"); //讀取xml檔案
$newxml = $xml->asXML(); //標準化$xml
$fp = fopen("newxml.xml", "w"); //建立xml檔案
fwrite($fp, $newxml); //寫入-------xml檔案
fclose($fp);

PHP可以方便的產生和讀取XML檔案。PHP主要通過DOMDocument、DOMElement和DOMNodeList來完成XML的讀取與寫入操作的。下面就簡要說明下如何使用這些類。

一.產生XML檔案
對於一個如下XML檔案。

[html] <?xml version="1.0" encoding="UTF-8"?>
<article>
<title>PHP訪問MySql資料庫 初級篇</title>
<link>http://blog.csdn.net/morewindows/article/details/7102362</link>
</article>
<?xml version="1.0" encoding="UTF-8"?>
<article>
<title>PHP訪問MySql資料庫 初級篇</title>
<link>http://blog.csdn.net/morewindows/article/details/7102362</link>
</article>
我們來看看如何用PHP來產生:

首先new一個DOMDocument對象並設定編碼格式。

$dom = newDOMDocument('1.0', 'UTF-8');

$dom->formatOutput= true;

再建立<article>結點和<title>結點

$rootelement =$dom->createElement("article");

$title =$dom->createElement("title", "PHP訪問MySql資料庫 初級篇");

然後建立帶常值內容的<link>結點

$link =$dom->createElement("link","http://blog.csdn.net/morewindows/article/details/7102362");

也可以先產生<link>結點再為其添加常值內容。

$link = $dom->createElement("link");

$linktext =$dom->createTextNode('http://blog.csdn.net/morewindows/article/details/7102362');

$link->appendChild($linktext);

然後將<title>和<link>結點加入到<article>結點中去

$rootelement->appendChild($title);

$rootelement->appendChild($link);

最後將<article>結點加入到DOMDocument對象中,

$dom->appendChild($rootelement);

這樣一個完整的XML就產生完畢了。再整出整個XML,

echo $dom->saveXML() ;

saveXML()也可以只輸入部分XML文本,如echo $dom->saveXML($link);就只會輸出<link>結點:<link>http://blog.csdn.net/morewindows/article/details/7102362</link>

下面再給出一個完整的PHP中資料內容輸出到XML檔案的例子。該例子會對將一個PHP數組輸出到XML檔案中。

[php] <?php
//將數組輸出到XML檔案中
// by MoreWindows( http://blog.csdn.net/MoreWindows )
$article_array = array(
"第一篇" => array(
"title"=>"PHP訪問MySql資料庫 初級篇",
"link"=>"http://blog.csdn.net/morewindows/article/details/7102362"
),
"第二篇" => array(
"title"=>"PHP訪問MySql資料庫 中級篇 Smarty技術",
"link"=>"http://blog.csdn.net/morewindows/article/details/7094642"
),
"第三篇" => array(
"title"=>"PHP訪問MySql資料庫 進階篇 AJAX技術",
"link"=>"http://blog.csdn.net/morewindows/article/details/7086524"
),
);
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$rootelement = $dom->createElement("MoreWindows");
foreach ($article_array as $key=>$value)
{
$article = $dom->createElement("article", $key);
$title = $dom->createElement("title", $value['title']);
$link = $dom->createElement("link", $value['link']);
$article->appendChild($title);
$article->appendChild($link);
$rootelement->appendChild($article);
 

}
$dom->appendChild($rootelement);
$filename = "D:test.xml";
echo 'XML檔案大小' . $dom->save($filename) . '位元組';
?>
<?php
//將數組輸出到XML檔案中
// by MoreWindows( http://blog.csdn.net/MoreWindows )
$article_array = array(
"第一篇" => array(
"title"=>"PHP訪問MySql資料庫 初級篇",
"link"=>"http://blog.csdn.net/morewindows/article/details/7102362"
),
"第二篇" => array(
"title"=>"PHP訪問MySql資料庫 中級篇 Smarty技術",
"link"=>"http://blog.csdn.net/morewindows/article/details/7094642"
),
"第三篇" => array(
"title"=>"PHP訪問MySql資料庫 進階篇 AJAX技術",
"link"=>"http://blog.csdn.net/morewindows/article/details/7086524"
),
);
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$rootelement = $dom->createElement("MoreWindows");
foreach ($article_array as $key=>$value)
{
$article = $dom->createElement("article", $key);
$title = $dom->createElement("title", $value['title']);
$link = $dom->createElement("link", $value['link']);
$article->appendChild($title);
$article->appendChild($link);
$rootelement->appendChild($article);
}
$dom->appendChild($rootelement);
$filename = "D:test.xml";
echo 'XML檔案大小' . $dom->save($filename) . '位元組';
?>
運行該PHP會在D盤上產生test.xml檔案(Win7 + XAMPP + IE9.0測試通過)

 

二.讀取XML檔案
以讀取前文中產生的D:test.xml為例:

[php] <?php
//讀取XML檔案
// by MoreWindows( http://blog.csdn.net/MoreWindows )
$filename = "D:test.xml";
$article_array = array();

$dom = new DOMDocument('1.0', 'UTF-8');
$dom->load($filename);

//得到<article>結點
$articles = $dom->getElementsByTagName("article");
echo '<article> 結點個數 ' . $articles->length;
foreach ($articles as $article)
{
$id = $article->getElementsByTagName("id")->item(0)->nodeValue;
$title = $article->getElementsByTagName("title")->item(0)->nodeValue;
$link = $article->getElementsByTagName("link")->item(0)->nodeValue;
$article_array[$id] = array('title'=>$title, 'link'=>$link);
}

//輸出結果
echo "<pre>";
var_dump($article_array);
echo "</pre>";
?>
<?php
//讀取XML檔案
// by MoreWindows( http://blog.csdn.net/MoreWindows )
$filename = "D:test.xml";
$article_array = array();

 

$dom = new DOMDocument('1.0', 'UTF-8');
$dom->load($filename);

//得到<article>結點
$articles = $dom->getElementsByTagName("article");
echo '<article> 結點個數 ' . $articles->length;
foreach ($articles as $article)
{
$id = $article->getElementsByTagName("id")->item(0)->nodeValue;
$title = $article->getElementsByTagName("title")->item(0)->nodeValue;
$link = $article->getElementsByTagName("link")->item(0)->nodeValue;
$article_array[$id] = array('title'=>$title, 'link'=>$link);
}

//輸出結果
echo "<pre>";
var_dump($article_array);
echo "</pre>";
?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.