php生成XML檔案步驟
阿新 • • 發佈:2018-11-19
步驟
1、例項化 DOMDocument類
2、創在節點
3、節點套節點
4、追加屬性內容等
這是方法裡面的全部內容:
$doc = new \DOMDocument('1.0','utf-8');//引入類並且規定版本編碼 $grandFather = $doc->createElement("Grandfather");//建立節點 $grandFather->setAttribute("ID","1");//給Grandfather增加ID屬性 $grandFather->setAttribute("name","王海洋");//給Grandfather增加NAME屬性 $father = $doc->createElement("Father");//建立節點 $father->setAttribute("son","王海");//給Father增加SON屬性 $grandFather->appendChild($father);//講Father放到Grandfather下 $content = $doc -> createTextNode('我是測試的');//設定標籤內容 $father -> appendChild($content);//將標籤內容賦給標籤 $doc->appendChild($grandFather);//建立頂級節點 $doc->save("C:/Users/k/Desktop/程式碼測試區/生成的XML檔案/test.xml");//儲存程式碼