PHP通過DOM解析XML檔案或者xml字串_PHP教程

來源:互聯網
上載者:User
上一篇記錄了php建立xml文檔的過程,這篇將記錄如何把接受到的xml字串中的資料和屬性值解析出來,以便進一步對資料進行處理。

xml檔案

1.

2.

3.

4.

5.

6.

7. spot411

8. spot411

9.

10.

11.

12.

13. Pmlal

14. Pmlal

15. Pmlal

16. Pmlal

17. Pmlal

18. Pmlal

19.

20.

21.

22.

23. Teasavant

24. Teasavant

25. Teasavant

26.

27.

28. Teasavant

29. Teasavant

30. Teasavant

31. Teasavant

32. Teasavant

33.

34.

35. Red8interactive

36. Red8interactive

37. Red8interactive

38. Red8interactive

39. Red8interactive

40. Red8interactive

41. Red8interactive

42. Red8interactive

43.

44.

45.

46.

47.

1. $xml = new DOMDocument();

2.

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

4. $xmlstring = 'spot411spot411PmlalPmlalPmlalPmlalPmlalPmlalTeasavantTeasavantTeasavantTeasavantTeasavantTeasavantTeasavantTeasavantRed8interactiveRed8interactiveRed8interactiveRed8interactiveRed8interactiveRed8interactiveRed8interactiveRed8interactive';

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."
";

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."
";

17. //echo $worker_att_id."
";

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."
";

25. }

26. }

27. }

如果是xml檔案,則用

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

如果是解析xml字串則用

$xml->loadXML($xmlstring);

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

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

獲得節點值:nodeValue;

本文出自 “Bob” 部落格

http://www.bkjia.com/PHPjc/478703.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478703.htmlTechArticle上一篇記錄了php建立xml文檔的過程,這篇將記錄如何把接受到的xml字串中的資料和屬性值解析出來,以便進一步對資料進行處理。 xml檔案...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.