-
-
$xmlpatch = 'index.xml';
- $_id = '1';
- $_title = 'title1';
- $_content = 'content1';
- $_author = 'author1';
- $_sendtime = 'time1';
- $_htmlpatch = '1.html';
- $doc = new DOMDoDoment ('1.0', 'utf-8');
- $doc -> formatOutput = true;
- $root = $doc -> createElement_x('root');//新節點
- $index = $doc -> createElement_x('index');//新節點
- $url = $doc -> createAttribute('url');//新屬性
- $patch = $doc -> createTextNode($_htmlpatch );//新TEXT值
- $url -> appendChild($patch);//將$patch文字設為$url屬性的值
- $id = $doc -> createAttribute('id');
- $newsid = $doc -> createTextNode($_id);
- $id -> appendChild($newsid);
- $title = $doc -> createAttribute('title');
- $ newstitle = $doc -> createTextNode($_title);
- $title -> appendChild($newstitle);
- $content = $doc -> createTextNode($_content);//節點值
- $author = $doc -> createAttribute('author');
- $newsauthor = $doc -> createTextNode($_author);
- $author -> appendChild($newsauthor);
- $sendtime = $docdoc -> appendChild($newsauthor);
- $sendtime = $docdoc - > createAttribute('time');
- $newssendtime = $doc -> createTextNode($_sendtime);
- $sendtime -> appendChild($newssendtime);
- $index -> appendChild($id); //將$id設為index節點的屬性,以下類別同
- $index -> appendChild($title);
- $index -> appendChild($content);
- $index -> appendChild( $url);
- $index -> appendChild($author);
- $index -> appendChild($sendtime);
- $root -> appendChild($index);//設定index為root節點
- $doc -> appendChild($root);//設定root為跟節點
- $doc -> save($xmlpatch);//儲存檔案
- echo $xmlpatch . ' has create success ';
- ?>
-
-
-
- XML操作
-
-
-
複製程式碼
-
-
-
增加功能(跟index.php檔案差不多,主要就是加個load載入跟$root = $doc -> documentElement取得跟節點
-
$xmlpatch = 'index.xml';
- $_id = '2';
- $_title = 'title2';
- $_content = 'content2';
- $_author = 'author2';
- $_sendtime = 'time2';
- $_htmlpatch = '2.html';
- $doc = new DOMDocument();
- $doc -> formatOutput = true;
- if($doc -> load($xmlpatch)) {
- $root = $doc -> documentElement;//取得根節點(root)
- $index = $doc -> createElement_x('index');
- $url = $doc -> createAttribute('url') ;
- $patch = $doc -> createTextNode($_htmlpatch);
- $url -> appendChild($patch);
- $id = $doc -> createAttribute('id');
- $newsid = $doc -> createTextNode($_id);
- $id -> appendChild($newsid);
- $title = $doc -> createAttribute('title');
- $newstitle = $ doc -> createTextNode($_title);
- $title -> appendChild($newstitle);
- $content = $doc -> createTextNode($_content);
- $author = $doc -> createAttribute( 'author');
- $newsauthor = $doc -> createTextNode($_author);
- $author -> appendChild($newsauthor);
- $sendtime = $doc -> createAttribute('time') ;
- $newssendtime = $doc -> createTextNode($_sendtime);
- $sendtime -> appendChild($newssendtime);
- $index -> appendChild($id);
- $index -> appendChild($title);
- $index -> appendChild($content);
- $index -> appendChild($url);
- $index -> appendChild($author);
- $index -> appendChild($sendtime);
- $root -> appendChild($index);
- $doc -> save($xmlpatch);
- echo $_id . ' has been added in ' . $xmlpatch ;
- } else {
- echo 'xml file loaded error!';
- }
- ?>
-
-
-
- XML操作-新增
-
-
-
-
複製程式碼
3edit.php 修改功能(只修改title屬性值跟節點值) 1 2 下一頁尾頁
|