PHP parses XML files or xml strings through DOM

Source: Internet
Author: User

 

 

The previous article recorded the process of creating an xml document in php. This article will record how to parse the data and attribute values in the received xml string for further processing.

Xml file

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.

8.

9. </worker>

10. </time>

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

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

13.

14.

15.

16.

17.

18.

19. </worker>

20. </time>

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

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

23.

24.

25.

26. </worker>

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

28.

29.

30.

31.

32.

33. </worker>

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

35.

36.

37.

38.

39.

40.

41.

42.

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

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 .}

If it is an xml file, use

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

For parsing xml strings, use

$ Xml-> loadXML ($ xmlstring );

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

Get node attributes: getAttribute ('id ');

Obtain the node value: nodeValue;

This article is from the "Bob" blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.