隨筆 - 8  文章 - 55  trackbacks - 0
          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          朋友的Blog

          最新評論

          閱讀排行榜

          評論排行榜

          使用PHP5 DOM-XML創建和解析XML文件

          ?
          由 Cloud 在 周二, 2006-09-12 01:44 提交

          先用PHP5創建一個xml文件

          <?php
          $dom = new DomDocument("1.0");
          
          $root = $dom -> createElement("html");
          $title = $dom -> createElement("title");
          $meta = $dom -> createElement("meta");
          $head = $dom -> createElement("head");
          $titleText = $dom -> createTextNode("this is a title");
          $metaText = $dom -> createTextNode("this is a meta");
          $table = $dom -> createElement("table");
          $tr = $dom -> createElement("tr");
          $td = $dom -> createElement("td");
          $tdText = $dom -> createTextNode("words");
          
          $root = $dom -> appendChild($root);
          $head = $root -> appendChild($head);
          $title = $head -> appendChild($title);
          $meta = $head -> appendChild($meta);
          $comment = $title -> appendChild($titleText);
          $meta -> appendChild($metaText);
          
          $td -> appendChild($tdText);
          $tr -> appendChild($td);
          $table -> appendChild($tr);
          $root -> appendChild($table);
          $dom -> save("test5.xml");
          
          
          echo "<hr/><a href=\"test5.xml\">查看test5.xml</a>";
          
          ?>
          

          test5.xml

          <?xml version="1.0"?>
          <html>
          <head>
          <title>this is a title</title>
          <meta>this is a meta</meta>
          </head>
          <table>
          <tr>
          <td>words</td>
          </tr>
          </table>
          </html>
          

          解析test5.xml

          <?php
          //首先要創建一個DOMDocument對象
          $dom = new DomDocument("1.0");
          
          //然后載入XML文件
          $dom -> load("test5.xml");
          
          //向DOM中寫入新數據
          $tr = $dom -> createElement("tr");
          $td = $dom -> createElement("td");
          $tdText = $dom -> createTextNode("hello world");
          $td -> appendChild($tdText);
          $tr -> appendChild($td);
          $dom -> documentElement -> getElementsByTagName("table") -> item(0) -> appendChild($tr);
          
          //向DOM中寫入新數據
          $tr2 = $dom -> createElement("tr");
          $td2 = $dom -> createElement("td");
          $tdText2 = $dom -> createTextNode("hello world too");
          $td2 -> appendChild($tdText2);
          $tr2 -> appendChild($td2);
          $xpath = new domxpath($dom);
          $trs = $xpath -> query("/html/table");
          $trs -> item(0) -> appendChild($tr2);
          
          $dom -> save("newfile.xml");
          
          echo "<hr/><a href=\"newfile.xml\">查看newfile.xml</a>";
          
          print "<hr>取得所有的td元素<br>";
          
          $tds = $dom -> getElementsByTagName("td");
          foreach ($tds as $nodes)
          {
          	print $nodes -> textContent."<br>";
          }
          
          echo "<hr/>使用XPath查詢的td節點結果:<hr/>";
          $tdss = $xpath -> query("/html/table/tr");
          foreach ($tdss as $nodes)
          {
          	print $nodes -> textContent."<br>";
          }
          ?>
          

          ( categories: PHP )
          由 Marchday 在 周二, 2006-09-12 13:52 提交

          <?php
          header("Content-Type: text/xml");
          
          $doc = new DOMDocument('1.0');
          // we want a nice output
          //$doc->formatOutput = true;
          $root = $doc->createElement('book');
          $root = $doc->appendChild($root);
          $title = $doc->createElement('title');
          $title = $root->appendChild($title);
          $text = $doc->createTextNode('This is the title');
          $text = $title->appendChild($text);
          //echo "Saving all the document:\n";
          echo $doc->saveXML() . "\n";
          //echo "Saving only the title part:\n";
          //echo $doc->saveXML($title) . "\n";
          ?> 
          

          注意不要忘了 header("Content-Type:text/xml") 哦

          posted on 2006-10-10 08:37 blog搬家了--[www.ialway.com/blog] 閱讀(827) 評論(0)  編輯  收藏 所屬分類: PHP
          主站蜘蛛池模板: 巩留县| 呼伦贝尔市| 象山县| 赤城县| 浦县| 西畴县| 略阳县| 定州市| 吴桥县| 肃宁县| 澄江县| 富宁县| 宁明县| 南城县| 额济纳旗| 阳原县| 南充市| 寿宁县| 明水县| 边坝县| 太仓市| 和政县| 茂名市| 昌吉市| 福州市| 威远县| 道孚县| 和田市| 和平区| 兰溪市| 淮滨县| 武定县| 南投市| 阿坝| 突泉县| 沁阳市| 方正县| 德惠市| 陇南市| 兴仁县| 修武县|