Php parses XML document attributes and edits the code

Source: Internet
Author: User
Php parses XML document attributes and edits the code

  1. // Read xml
  2. $ Dom = new DOMDocument ('1. 0 ');
  3. $ Dom-> load ('data. XML ');
  4. $ Em = $ dom-> getElementsByTagName ('videoos '); // outermost node
  5. $ Em = $ em-> item (0 );
  6. $ Items = $ em-> getElementsByTagName ('video'); // Node
  7. // Remove the following section if you do not need to read it.
  8. Foreach ($ items as $ ){
  9. Foreach ($ a-> attributes as $ B) {// $ B-> nodeValue; node attribute value $ B-> nodeName; node attribute name
  10. Echo $ B-> nodeName;
  11. Echo ":";
  12. Echo $ B-> nodeValue;
  13. Echo"
    ";
  14. }
  15. }
  16. // Below is a new line written to xml
  17. $ T = $ dom-> createElement ('video ');// $ T-> setAttribute ('title', '1 ');// $ T-> setAttribute ('src', '2 ');// $ T-> setAttribute ('IMG ', '1 ');// $ Em-> appendChild ($ t );//
  18. $ Dom-> save ('data. XML ');
  19. ?>

The xml document at that time:

The xml file can be modified as follows:

  1. $ Doc = new DOMDocument ();
  2. $ Doc-> load ('data. XML ');
  3. // Find the videos node
  4. $ Root = $ doc-> getElementsByTagName ('videoos ');
  5. // The 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 attributes of each video node
  13. Foreach ($ rootdata-> attributes as $ attrib)
  14. {
  15. $ AttribName = $ attrib-> nodeName; // nodeName is the attribute name.
  16. $ AttribValue = $ attrib-> nodeValue; // nodeValue indicates the attribute content.
  17. // Search for the node content whose property name is ip
  18. If ($ attribName = 'IMG ')
  19. {
  20. // Find the node content whose property content is ip
  21. If ($ attribValue = '1 ')
  22. {
  23. // Change the property to img and the content of img to 1 to image;
  24. $ Rootdata-> setAttribute ('IMG ', 'image ');
  25. $ Doc-> save ('data. XML ');
  26. }
  27. }
  28. }
  29. }
  30. ?>

Recommended for script School editing: php reading xml Class php xml Document parsing function learning instance php parsing XML data a piece of code PHP reading XML several methods php using DOM reading XML file code example learning php operating 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.