PHP parsing XML document properties and editing the code

Source: Internet
Author: User
Tags learn php
  1. Read XML
  2. $dom =new DOMDocument (' 1.0 ');
  3. $dom->load (' Data.xml ');
  4. $em = $dom->getelementsbytagname (' videos ');//Outermost node
  5. $em = $em->item (0);
  6. $items = $em->getelementsbytagname (' video ');//Node
  7. If you do not need to read directly add the following paragraph can be removed
  8. foreach ($items as $a) {
  9. foreach ($a->attributes as $b) {//$b the value of the->nodevalue; node property $b->nodename; The name of the node property
  10. Echo $b->nodename;
  11. echo ":";
  12. Echo $b->nodevalue;
  13. echo "
    ";
  14. }
  15. }
  16. The following is a new write to the XML line
  17. $t = $dom->createelement (' video ');//<>
  18. $t->setattribute (' title ', ' 1 ');//
  19. $t->setattribute (' src ', ' 2 ');//
  20. $t->setattribute (' img ', ' 1 ');//
  21. $em->appendchild ($t);//
  22. $dom->save (' Data.xml ');
  23. ?>
Copy Code

XML document at the time:

Copy Code

The following is the last change to modify the XML file:

  1. $doc = new DOMDocument ();
  2. $doc->load (' Data.xml ');
  3. Find Videos nodes
  4. $root = $doc->getelementsbytagname (' videos ');
  5. First Videos node
  6. $root = $root->item (0);
  7. Find the video node under the videos node
  8. $userid = $root->getelementsbytagname (' video ');
  9. Traverse All video nodes
  10. foreach ($userid as $rootdata)
  11. {
  12. Traverse all properties of each video node
  13. foreach ($rootdata->attributes as $attrib)
  14. {
  15. $attribName = $attrib->nodename; NodeName is the property name
  16. $attribValue = $attrib->nodevalue; NodeValue as attribute content
  17. Find node content with property name IP
  18. if ($attribName = = ' img ')
  19. {
  20. Find the content of the node with the property content as IP
  21. if ($attribValue = = ' 1 ')
  22. {
  23. Modify the property to img,img content of 1 to image;
  24. $rootdata->setattribute (' img ', ' image ');
  25. $doc->save (' Data.xml ');
  26. }
  27. }
  28. }
  29. }
  30. ?>
Copy Code

Scripting Academy Editor's recommendation: PHP Read XML class PHP XML document parsing function Learning instance PHP parsing XML data A piece of code PHP read XML several ways PHP uses the DOM class to read the XML file of the code example to learn PHP operation XML class DOMDocument

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