$dom = new DomDocument("1.0"); // 建立物件, 用於控制XML
$xmlfile = "favorite/".$_SESSION['login_id'].".xml";
if(file_exists($xmlfile))
{
$dom -> load($xmlfile);
}
else
{
$root = $dom -> createElement("favorites");
$dom -> appendChild($root);
}
if(isset($_POST['web_name']))
{
$web_name = $_POST['web_name'];
$web_address = $_POST['address'];
if(!ereg("://",$web_address))
$web_address = "http://".$web_address;
$favorite = $dom -> createElement("favorite"); // 建立節點
$name = $dom -> createElement("name");
$address = $dom -> createElement("address");
$nameText = $dom -> createTextNode($web_name); // 建立節點內容
$addressText = $dom -> createTextNode($web_address);
$name -> appendChild($nameText); // 導入節點,這時候才算把節點加進去
$address -> appendChild($addressText);
$favorite -> appendChild($name);
$favorite -> appendChild($address);
$dom -> getElementsByTagName("favorites") -> item(0) -> appendChild($favorite); //修改舊的XML 檔案...跟建立一個新的是不一樣的.. 另外增加節點...如果不用ITEM(0)..不能加入
$dom -> save($xmlfile);
}
$favorite = $dom -> getElementsByTagName("favorite"); // 取得$dom 之下所有 FAVORITE 節點 的陣列...
foreach ($favorite as $favo)
{
$name= $favo -> getElementsByTagName("name");
$address = $favo -> getElementsByTagName("address");
$address = $address -> item(0)->nodeValue;
$name = $name -> item(0)->nodeValue;
echo '<a href="'.$address.'">'.$name.'</a><br />';
}
參考::http://www.phpx.com/happy/thread-137323-1-1.html
http://luckynet.1stfreehosting.com/programming/php/php5gb/ref.dom.html
http://hi.baidu.com/noirwinter/blog/item/732a6ad00a23b38fa0ec9c4a.html
http://www.blogjava.net/sl2cj/articles/74208.html
http://www.diybl.com/course/4_webprogram/php/phpshil/2008618/126506.html