PHP可以方便的產生和讀取XML檔案。PHP主要通過DOMDocument、DOMElement和DOMNodeList來完成XML的讀取與寫入操作的。下面就簡要說明下如何使用這些類。
一.產生XML檔案
對於一個如下XML檔案。
[html]
PHP訪問MySql資料庫 初級篇
http://blog.csdn.net/morewindows/article/details/7102362
PHP訪問MySql資料庫 初級篇
http://blog.csdn.net/morewindows/article/details/7102362
我們來看看如何用PHP來產生:
首先new一個DOMDocument對象並設定編碼格式。
$dom = newDOMDocument('1.0', 'UTF-8');
$dom->formatOutput= true;
再建立結點和結點</p>
$rootelement =$dom->createElement("article");
$title =$dom->createElement("title", "PHP訪問MySql資料庫 初級篇");
然後建立帶常值內容的結點
$link =$dom->createElement("link","http://blog.csdn.net/morewindows/article/details/7102362");
也可以先產生結點再為其添加常值內容。
$link = $dom->createElement("link");
$linktext =$dom->createTextNode('http://blog.csdn.net/morewindows/article/details/7102362');
$link->appendChild($linktext);
然後將和結點加入到結點中去
$rootelement->appendChild($title);
$rootelement->appendChild($link);
最後將結點加入到DOMDocument對象中,
$dom->appendChild($rootelement);
這樣一個完整的XML就產生完畢了。再整出整個XML,
echo $dom->saveXML() ;
saveXML()也可以只輸入部分XML文本,如echo $dom->saveXML($link);就只會輸出結點:http://blog.csdn.net/morewindows/article/details/7102362
下面再給出一個完整的PHP中資料內容輸出到XML檔案的例子。該例子會對將一個PHP數組輸出到XML檔案中。
[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) . '位元組';
?>
//將數組輸出到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] //讀取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);
//得到結點
$articles = $dom->getElementsByTagName("article");
echo ' 結點個數 ' . $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 "
";
var_dump($article_array);
echo "
";
?>
//讀取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);
//得到結點
$articles = $dom->getElementsByTagName("article");
echo ' 結點個數 ' . $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 "
";
var_dump($article_array);
echo "
";
?>
運行結果如下:
摘自 MoreWindows
http://www.bkjia.com/PHPjc/478414.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478414.htmlTechArticlePHP可以方便的產生和讀取XML檔案。PHP主要通過DOMDocument、DOMElement和DOMNodeList來完成XML的讀取與寫入操作的。下面就簡要說明下如何使用這些...