PHP read XML method One---domdocument read xml

Source: Internet
Author: User
XML (extensible Markup Language) extends the markup language , which, like HTML, is SGML (Standard generalized Markup Language, the standardized universal Markup language).
  1. XML source file
  2. Zhang Ying
  3. Man
  4. 28
  5. Tank
  6. Man
  7. 28
  8. $doc = new DOMDocument ();
  9. $doc->load (' person.xml '); Reading an XML file
  10. $humans = $doc->getelementsbytagname ("humans"); An array of objects that get humans tags
  11. foreach ($humans as $human)
  12. {
  13. $names = $human->getelementsbytagname ("name"); An array of objects that get the label of name
  14. $name = $names->item (0)->nodevalue; Gets the value in node, such as
  15. $sexs = $human->getelementsbytagname ("sex");
  16. $sex = $sexs->item (0)->nodevalue;
  17. $olds = $human->getelementsbytagname ("old");
  18. $old = $olds->item (0)->nodevalue;
  19. echo "$name-$sex-$old \ n";
  20. }
  21. ?>
Copy Code
  • 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.