PHP parsing XML files or XML strings through Dom _php tutorial

Source: Internet
Author: User
The previous article documented the PHP process of creating an XML document, which records how data and attribute values in the received XML string are parsed for further processing of the data.

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.

21st.

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 Red8inter Active 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) {

$time _att_value = $times->getattribute ("value");

echo $time _att_value. "
";

$d _worker = $times->getelementsbytagname ("worker");

. foreach ($d _worker as $d _work) {

$worker _att_id = $d _work->getattribute ("id");

$worker _att_name = $d _work->getattribute ("name");

echo $worker _att_name. "
";

//echo $worker _att_id. "
";

$d _hours = $d _work->getelementsbytagname ("hour");

. foreach ($d _hours as $d _hour) {

$hour _att_pid = $d _hour->getattribute ("pid");

$hour _att_pmid = $d _hour->getattribute ("PMID");

22.

. Echo Count ($d _hour);

echo $d _hour->nodevalue. "
";

25.}

26.}

27.}

If it is an XML file, use the

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

If parsing an XML string, use the

$xml->loadxml ($xmlstring);

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

Get Node Properties: getattribute (' id ');

Get node value: nodevalue;

This article is from "Bob" blog

http://www.bkjia.com/PHPjc/478703.html www.bkjia.com true http://www.bkjia.com/PHPjc/478703.html techarticle The previous article documented the PHP process of creating an XML document, which records how data and attribute values in the received XML string are parsed for further processing of the data. XML file ...

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