程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP通過DOM解析XML文件或者xml字符串

PHP通過DOM解析XML文件或者xml字符串

編輯:關於PHP編程

 

 

上一篇記錄了php創建xml文檔的過程,這篇將記錄如何把接受到的xml字符串中的數據和屬性值解析出來,以便進一步對數據進行處理。

xml文件

1.    <?xml version="1.0" encoding="utf-8"?>

2.    <data>

3.    <time value="2011-08-01"/>

4.    <time value="2011-08-02"/>

5.    <time value="2011-08-03">

6.    <worker name="test1" id="14">

7.    <hour pid="6" pmid="3"><![CDATA[spot411]]></hour>

8.    <hour pid="6" pmid="3"><![CDATA[spot411]]></hour>

9.    </worker>

10.   </time>

11.   <time value="2011-08-04">

12.   <worker name="test2" id="6">

13.   <hour pid="9" pmid="4"><![CDATA[Pmlal]]></hour>

14.   <hour pid="9" pmid="4"><![CDATA[Pmlal]]></hour>

15.   <hour pid="9" pmid="4"><![CDATA[Pmlal]]></hour>

16.   <hour pid="9" pmid="4"><![CDATA[Pmlal]]></hour>

17.   <hour pid="9" pmid="4"><![CDATA[Pmlal]]></hour>

18.   <hour pid="9" pmid="4"><![CDATA[Pmlal]]></hour>

19.   </worker>

20.   </time>

21.   <time value="2011-08-05">

22.   <worker name="test3" id="1">

23.   <hour pid="1" pmid="1"><![CDATA[Teasavant]]></hour>

24.   <hour pid="1" pmid="1"><![CDATA[Teasavant]]></hour>

25.   <hour pid="1" pmid="1"><![CDATA[Teasavant]]></hour>

26.   </worker>

27.   <worker name="tony" id="2">

28.   <hour pid="1" pmid="1"><![CDATA[Teasavant]]></hour>

29.   <hour pid="1" pmid="1"><![CDATA[Teasavant]]></hour>

30.   <hour pid="1" pmid="1"><![CDATA[Teasavant]]></hour>

31.   <hour pid="1" pmid="1"><![CDATA[Teasavant]]></hour>

32.   <hour pid="1" pmid="1"><![CDATA[Teasavant]]></hour>

33.   </worker>

34.   <worker name="test4" id="4">

35.   <hour pid="3" pmid="3"><![CDATA[Red8interactive]]></hour>

36.   <hour pid="3" pmid="3"><![CDATA[Red8interactive]]></hour>

37.   <hour pid="3" pmid="3"><![CDATA[Red8interactive]]></hour>

38.   <hour pid="3" pmid="3"><![CDATA[Red8interactive]]></hour>

39.   <hour pid="3" pmid="3"><![CDATA[Red8interactive]]></hour>

40.   <hour pid="3" pmid="3"><![CDATA[Red8interactive]]></hour>

41.   <hour pid="3" pmid="3"><![CDATA[Red8interactive]]></hour>

42.   <hour pid="3" pmid="3"><![CDATA[Red8interactive]]></hour>

43.   </worker>

44.   </time>

45.   <time value="2011-08-06"/>

46.   <time value="2011-08-07"/>

47.   </data>

1.            $xml = new DOMDocument();

2.            

3.            //$xml->load("timeline.xml");

4.            $xmlstring = '<data><time value="2011-08-01"/><time value="2011-08-02"/><time value="2011-08-03"><worker name="test1" id="14"><hour pid="6" pmid="3">spot411</hour><hour pid="6" pmid="3">spot411</hour></worker></time><time value="2011-08-04"><worker name="test2" id="6"><hour pid="9" pmid="4">Pmlal</hour><hour pid="9" pmid="4">Pmlal</hour><hour pid="9" pmid="4">Pmlal</hour><hour pid="9" pmid="4">Pmlal</hour><hour pid="9" pmid="4">Pmlal</hour><hour pid="9" pmid="4">Pmlal</hour></worker></time><time value="2011-08-05"><worker name="test3" id="1"><hour pid="1" pmid="1">Teasavant</hour><hour pid="1" pmid="1">Teasavant</hour><hour pid="1" pmid="1">Teasavant</hour></worker><worker name="test4" id="2"><hour pid="1" pmid="1">Teasavant</hour><hour pid="1" pmid="1">Teasavant</hour><hour pid="1" pmid="1">Teasavant</hour><hour pid="1" pmid="1">Teasavant</hour><hour pid="1" pmid="1">Teasavant</hour></worker><worker name="test5" id="4"><hour pid="3" pmid="3">Red8interactive</hour><hour pid="3" pmid="3">Red8interactive</hour><hour pid="3" pmid="3">Red8interactive</hour><hour pid="3" pmid="3">Red8interactive</hour><hour pid="3" pmid="3">Red8interactive</hour><hour pid="3" pmid="3">Red8interactive</hour><hour pid="3" pmid="3">Red8interactive</hour><hour pid="3" pmid="3">Red8interactive</hour></worker></time><time value="2011-08-06"/><time value="2011-08-07"/></data>';

5.            $xml->loadXML($xmlstring);

6.            

7.            $timedom = $xml->getElementsByTagName("time");

8.            $d_array = array();

9.            foreach ($timedom as $times){

10.               $time_att_value = $times->getAttribute("value");

11.               echo $time_att_value."<br>";

12.               $d_worker = $times->getElementsByTagName("worker");

13.               foreach ($d_worker as $d_work){

14.                   $worker_att_id = $d_work->getAttribute("id");

15.                   $worker_att_name = $d_work->getAttribute("name");

16.                   echo $worker_att_name."<br>";

17.                   //echo $worker_att_id."<br>";

18.                   $d_hours = $d_work->getElementsByTagName("hour");

19.                   foreach ($d_hours as $d_hour){

20.                       $hour_att_pid = $d_hour->getAttribute("pid");

21.                       $hour_att_pmid = $d_hour->getAttribute("pmid");

22.                      

23.                       echo count($d_hour);

24.                       echo $d_hour->nodeValue."<br>";

25.                   }

26.               }

27.           }

如果是xml文件,則用

$xml->load("timeline.xml");

如果是解析xml字符串則用

$xml->loadXML($xmlstring);

------------------------------------------------------

獲得節點屬性:getAttribute('id');

獲得節點值:nodeValue;

本文出自 “Bob” 博客

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved