国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP操作XML的知識點記錄DOMDocument

2024-05-04 21:48:11
字體:
來源:轉載
供稿:網友

在php應用中PHP操作XML是常常要用到的,在php中操作xml文檔有很多種方法,如DOMDocument就是一個對xml解析,讀寫,增加,刪除的好工具了,下面我來分享一下我對php xml理解與學習筆記吧.

1、XML文件內容,代碼如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <phoneBook>  
  3.     <person>  
  4.         <name>吳邊</name>  
  5.         <tel>13222053785</tel>  
  6.     </person>  
  7.     <person>  
  8.         <name>吳邊2</name>  
  9.         <tel>15151981159</tel>  
  10.     </person>  
  11.     <person>  
  12.         <name>test</name>  
  13.         <tel>15077887624</tel>  
  14.     </person>  
  15.     <person>  
  16.         <name>撒旦發撒旦發</name>  
  17.         <tel>132423142134</tel>  
  18.     </person>  
  19.     <person>  
  20.         <name>阿斯頓飛</name>  
  21.         <tel>1234123421342134</tel>  
  22.     </person>  
  23. </phoneBook> 

2、生成新的XML文件,代碼如下:

  1. $doc=new DOMDocument("1.0","utf-8");  #聲明文檔類型  
  2. $doc->formatOutput=true;              #設置可以輸出操作  
  3. #聲明根節點,最好一個XML文件有個跟節點  
  4. $root=$doc->createElement("phoneBook");#創建節點對象實體  
  5. $doc->appendChild($root);            #把節點添加進來  
  6.     
  7. $person=$doc->createElement("person");  #創建節點對象實體    
  8. $root->appendChild($person);    #把節點添加到root節點的子節點  
  9.     
  10. $contactName=$doc->createElement("name");  
  11. $person->appendChild($contactName);  
  12.     
  13. $contactNum=$doc->createElement("tel");  
  14. $person->appendChild($contactNum);  
  15.     
  16. $contactName->appendChild($doc->createTextNode($name));  
  17. $contactNum->appendChild($doc->createTextNode($tel));  
  18.     
  19. $doc->save($filename); #保存路徑 

3、XML文件添加節點,代碼如下:

  1. $doc = new DOMDocument();  #聲明文檔類型  
  2. $doc -> load($filename);  
  3. $root = $doc->getElementsByTagName("phoneBook")->item(0);  
  4.         
  5. $person = $doc->createElement("person");  
  6.     
  7. $contactName = $doc->createElement("name");  
  8. $contactName->nodeValue = $name;  
  9.     
  10. $contactNum = $doc->createElement("tel");  
  11. $contactNum->nodeValue = $tel;  
  12. //開源代碼Vevb.com 
  13. $person -> appendChild($contactName);  
  14. $person -> appendChild($contactNum);  
  15. $root -> appendChild($person);  
  16. $doc->save($filename); #保存路徑 

4、遍歷XML文件,代碼如下:

  1. $xmldoc = new DOMDocument();  
  2. $xmldoc -> load("contacts.xml");  
  3. $root = $xmldoc->getElementsByTagName("person");  
  4.     
  5. foreach($root as $v){  
  6.     echo $v->getElementsByTagName("name")->item(0)->nodeValue."<br/>";  

附上DOMDocument相關的內容. 

屬性:

Attributes 存儲節點的屬性列表(只讀) 

childNodes 存儲節點的子節點列表(只讀) 

dataType 返回此節點的數據類型 

Definition 以DTD或XML模式給出的節點的定義(只讀) 

Doctype 指定文檔類型節點(只讀) 

documentElement 返回文檔的根元素(可讀寫) 

firstChild 返回當前節點的第一個子節點(只讀) 

Implementation 返回XMLDOMImplementation對象 

lastChild 返回當前節點最后一個子節點(只讀) 

nextSibling 返回當前節點的下一個兄弟節點(只讀) 

nodeName 返回節點的名字(只讀) 

nodeType 返回節點的類型(只讀) 

nodeTypedValue 存儲節點值(可讀寫) 

nodeValue 返回節點的文本(可讀寫) 

ownerDocument 返回包含此節點的根文檔(只讀) 

parentNode 返回父節點(只讀) 

Parsed 返回此節點及其子節點是否已經被解析(只讀) 

Prefix 返回名稱空間前綴(只讀) 

preserveWhiteSpace 指定是否保留空白(可讀寫) 

previousSibling 返回此節點的前一個兄弟節點(只讀) 

Text 返回此節點及其后代的文本內容(可讀寫) 

url 返回最近載入的XML文檔的URL(只讀) 

Xml 返回節點及其后代的XML表示(只讀)

方法:

appendChild 為當前節點添加一個新的子節點,放在最后的子節點后 

cloneNode 返回當前節點的拷貝 

createAttribute 創建新的屬性 

createCDATASection 創建包括給定數據的CDATA段 

createComment 創建一個注釋節點 

createDocumentFragment 創建DocumentFragment對象 

createElement 創建一個元素節點 

createEntityReference 創建EntityReference對象 

createNode 創建給定類型,名字和命名空間的節點 

createPorcessingInstruction 創建操作指令節點 

createTextNode 創建包括給定數據的文本節點 

getElementsByTagName 返回指定名字的元素集合 

hasChildNodes 返回當前節點是否有子節點 

insertBefore 在指定節點前插入子節點 

Load 導入指定位置的XML文檔 

loadXML 導入指定字符串的XML文檔 

removeChild 從子結點列表中刪除指定的子節點 

replaceChild 從子節點列表中替換指定的子節點 

Save 把XML文件存到指定節點 

selectNodes 對節點進行指定的匹配,并返回匹配節點列表 

selectSingleNode 對節點進行指定的匹配,并返回第一個匹配節點 

transformNode 使用指定的樣式表對節點及其后代進行轉換 

transformNodeToObject 使用指定的樣式表將節點及其后代轉換 

實例獲取標簽屬性.值.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临邑县| 龙门县| 抚顺市| 城口县| 永吉县| 农安县| 专栏| 西峡县| 两当县| 大港区| 肇州县| 榆树市| 横山县| 巍山| 漯河市| 垫江县| 忻州市| 师宗县| 左云县| 曲麻莱县| 定结县| 安吉县| 遂溪县| 旅游| 南康市| 江津市| 孟村| 托克逊县| 砀山县| 宝应县| 旌德县| 靖西县| 博罗县| 鞍山市| 北宁市| 岳阳县| 湘潭县| 射洪县| 龙海市| 抚顺市| 富民县|