当前位置:编程学习 > XML/UML >>

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

 

 

上一篇记录了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. 

补充:Web开发 , php ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,