<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                上一節,講了Xml文件基本語法及元素,實體及Dtd約束技術,下面就正式進入PHP的Xml編程 ### 使用PHP技術對Xml文件進行操作 常用的有以下三種技術: 1、PHP dom 2、PHP結合XPath操作Xml 3、Simplexml技術 首先來說明Dom Dom(Document object model)文檔-對象-模型,核心思想是把一個Xml文件,看成一個對象模型,然后通過對象的方式來操作Xml文件。 ### 1、創建一個Xml文件 在學習過程中發現,自己直接編寫的Xml文件,在調用Xml函數的時候總是不能被正確的識別,所以先用PHP來創建一個Xml文件,再進行操作,直接看代碼 ~~~ <?php //實例化一個DomDocument對象 $dom = new DomDocument('1.0', 'UTF-8'); //如果文件不存在就創建一個 if (!file_exists("xml_test.xml")){ header("Content-Type: text/plain"); $root = $dom->createElement("class"); $dom->appendChild($root); $dom->save("xml_test.xml"); } else{ $dom->load("xml_test.xml"); } print $dom->saveXML(); ?> ~~~ 在網頁上打開,執行會創建個xml_test.xml文件(這里有個問題是,根目錄的名字不能使用漢字,具體沒有明白如何去設置?知道的朋友給指點下,非常感謝!) ~~~ <?xml version="1.0" encoding="UTF-8"?> <class/> ~~~ ### 2、增加元素 ~~~ <?php //addElementsxml.php $dom = new DOMDocument("1.0", "UTF-8"); $dom->load("xml_test.xml"); $root_class = $dom->getElementsByTagName("class"); $i = 0; // for($i = 0;$i < 4; $i++){ $root_class_node = $root_class->item($i); $stu_node=$dom->createElement("student"); $stu_node->setAttribute("xingbie", "man"); $stu_node_name=$dom->createElement("name","name".$i); //設置屬性 $stu_node_name->setAttribute("hel", "23"); $stu_node_age=$dom->createElement("age","21"); $stu_node_introduce=$dom->createElement("introduce","1111"); $stu_node->appendChild($stu_node_name); $stu_node->appendChild($stu_node_age); $stu_node->appendChild($stu_node_introduce); $root_class_node->appendChild($stu_node); // } $dom->save("xml_test.xml"); print $dom->saveXML(); ?> ~~~ ### 3、遍歷元素 ~~~ <?php //getNode.php //解析一個文件的步驟 //1創建一個對象,表示文檔 $dom = new DOMDocument("1.0", "UTF-8"); //2制定加載那個Xml,解析那個文件 $dom->load("xml_test.xml"); //3 獲取你關新節點 $stu_nodes = $dom->getElementsByTagName("student"); for ($i=0;$i<$stu_nodes->length;$i++){ //取出每個學生 $stu_node = $stu_nodes->item($i); for($i=0;$i<$stu_node->childNodes->length;$i++){ echo $stu_node->childNodes->item($i)->nodeValue; echo "<br/>"; } } ?> ~~~ ### 4、刪除元素 ~~~ <?php //解析一個文件的步驟 //1創建一個對象,表示文檔 $dom = new DOMDocument("1.0", "UTF-8"); //2制定加載那個Xml,解析那個文件 $dom->load("xml_test.xml"); //3 獲取你關新節點 $stu_nodes = $dom->getElementsByTagName("student"); $stu_node=$stu_nodes->item($stu_nodes->length -1); $stu_node->parentNode->removeChild($stu_node); $dom->save("xml_test.xml"); ?> ~~~ ### 5、修改元素屬性 ~~~ <?php //1創建一個對象,表示文檔 $dom = new DOMDocument("1.0", "UTF-8"); //2制定加載那個Xml,解析那個文件 $dom->load("xml_test.xml"); //找到這個學生 $stus=$dom->getElementsByTagName("age")->item(0); $stus->nodeValue=100; $dom->save("xml_test.xml"); ?> ~~~ 上面介紹了基本DOM操作,增刪改查,下一節將有具體的實例來操作 參考資料: [http://www.php.net/manual/en/class.domdocument.php](http://www.php.net/manual/en/class.domdocument.php)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看