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. & lt ;? Xmlversion & quot; 1.0 & quot; encoding & quot; UTF-8 & quot ;? & Gt; 2. & lt; data & gt; 3. & lt; timevalue
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.
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 =' Spot411 Spot411 Pmlal Pmlal Pmlal Pmlal Pmlal Pmlal Teasavant Teasavant Teasavant Teasavant Teasavant Teasavant Teasavant Teasavant Red8interactive Red8interactive Red8interactive Red8interactive Red8interactive Red8interactive Red8interactive Red8interactive ';
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 .}
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