PHP uses Dom to operate XML

Source: Internet
Author: User

Php xml operation class domdocument attributes and Methods
Note that case sensitivity must not be incorrect.

Attribute:

  1. Attribute list of the attributes storage node (read-only)
  2. List of child nodes of the childnodes storage node (read-only)
  3. Datatype returns the Data Type of this node
  4. Definition defines nodes in DTD or XML mode (read-only)
  5. Doctype specifies the file type node (read-only)
  6. Documentelement returns the root element of the document (read/write)
  7. Firstchild returns the first child node of the current node (read-only)
  8. Implementation returns the xmldomimplementation object
  9. Lastchild returns the last child node of the current node (read-only)
  10. Nextsibling returns the next sibling node of the current node (read-only)
  11. Nodename: return the node name (read-only)
  12. Nodetype return node type (read-only)
  13. Nodetypedvalue stores node values (read/write)
  14. Nodevalue returns the node text (read/write)
  15. Ownerdocument returns the root document containing this node (read-only)
  16. Parentnode returns the parent node (read-only)
  17. Parsed: returns whether the node and Its subnodes have been parsed (read-only)
  18. Prefix returns the namespace prefix (read-only)
  19. Preservewhitespace specifies whether to retain white space (read/write)
  20. Previussibling returns the previous sibling node of this node (read-only)
  21. Text returns the text content of the node and its descendants (read/write)
  22. URL returns the URL of the recently loaded XML document (read-only)
  23. XML returns the XML Representation of the node and its descendants (read-only)

Method:

  1. Appendchild adds a new subnode to the current node, which is placed after the last subnode
  2. Clonenode returns the copy of the current node
  3. Createattribute
  4. Createcdatasection create CDATA segments that contain the given data
  5. Createcomment create a comment Node
  6. Createdocumentfragment create a documentfragment object
  7. Createelement: Create an element node
  8. Createentityreference: Create an entityreference object
  9. Createnode creates nodes of the given type, name, and namespace.
  10. Createporcessinginstruction
  11. Createtextnode: Create a text node that includes the given data
  12. Getelementsbytagname returns the set of elements with the specified name.
  13. Haschildnodes: returns whether the current node has subnodes.
  14. Insertbefore inserts a subnode before a specified Node
  15. Load import the XML document at the specified position
  16. Loadxml import the XML document of the specified string
  17. Removechild deletes a specified subnode from the subnode list.
  18. ReplaceChild replaces the specified child node from the child node list
  19. Save the XML file to the specified Node
  20. Selectnodes matches the specified node and returns the list of matched nodes.
  21. Selectsinglenode matches the specified node and returns the first matching node.
  22. Transformnode converts nodes and their descendants using the specified style table.
  23. Transformnodetoobject converts nodes and their descendants using the specified style table
<? PHP // read XML $ dom = new domdocument ('1. 0'); $ dom-> load ('data. XML '); $ em = $ dom-> getelementsbytagname ('videos'); // The outermost node $ em = $ em-> item (0 ); $ items = $ em-> getelementsbytagname ('video'); // node // if you do not need to read it, remove the following section to foreach ($ items as $) {foreach ($ A-> attributes as $ B) {// $ B-> nodevalue; node attribute value $ B-> nodename; node attribute name echo $ B-> nodename; echo ":"; echo $ B-> nodevalue; echo "<br/> ";}} // The following is a new $ T = $ dom-> createelement ('video '); // <Video $ T-> setattribute ('title', '1 '); // <video name = "data" $ T-> setattribute ('src', '2 '); // <video name = "data" src = "2" $ T-> setattribute ('img ', '1 '); // <video name = "data" IMG = "1" $ em-> appendchild ($ t ); // <video name = "data" IMG = "1"/> $ dom-> Save ('data. xml');?> The XML document at that time: <? XML version = "1.0"?> <Videos> <video IMG = "A" url = "1" Title = "1" nickname = "1" tag = "1" vid = "1" star = "1" /> <video IMG = "B" url = "2" Title = "2" nickname = "2" tag = "2" vid = "2" star = "2 "/> <video IMG = "C" url = "3" Title = "3" nickname = "3" tag = "3" vid = "3" star = "3"/> <video Title = "D" src = "2" IMG = "1"/> </videos> // the following file can be modified later. <? PHP $ Doc = new domdocument (); $ doc-> load ('data. xml'); // find the videos node $ root = $ doc-> getelementsbytagname ('videoos '); // The first videos node $ root = $ root-> item (0 ); // find the video node under the videos node $ userid = $ root-> getelementsbytagname ('video'); // traverse all video nodes foreach ($ userid as $ rootdata) {// traverse all attributes of each video node foreach ($ rootdata-> attributes as $ attrib) {$ attribname = $ attrib-> nodename; // nodename is the attribute name $ attribvalue = $ Attrib-> nodevalue; // nodevalue indicates the attribute content. // you can specify if ($ attribname = 'img ') as the node content whose attribute name is IP ') {// if ($ attribvalue = '1') {// modify the node content whose attribute is "IMG" and "IMG content is" 1 "to" image; $ rootdata-> setattribute ('img ', 'image'); $ doc-> Save ('data. xml') ;}}}?>

========================================================== ======================================

<? $ Xmldoc = new domdocument (); $ xmldoc-> load ("http: // localhost/XML. PHP "); $ x = $ xmldoc-> getelementsbytagname ('name'); For ($ I = 0; $ I <= $ X-> length-1; $ I ++) {If (strpos ($ X-> item ($ I)-> nodevalue, "fang ")! = False) {// determines whether the node content contains the character "fang", which can be used to search for ECHO $ X-> item ($ I) -> parentnode-> childnodes-> item (1)-> nodevalue ;}}?>

Php xml operation class domdocument attributes and Methods
Note that case sensitivity must not be incorrect.

Attribute:

  1. Attribute list of the attributes storage node (read-only)
  2. List of child nodes of the childnodes storage node (read-only)
  3. Datatype returns the Data Type of this node
  4. Definition defines nodes in DTD or XML mode (read-only)
  5. Doctype specifies the file type node (read-only)
  6. Documentelement returns the root element of the document (read/write)
  7. Firstchild returns the first child node of the current node (read-only)
  8. Implementation returns the xmldomimplementation object
  9. Lastchild returns the last child node of the current node (read-only)
  10. Nextsibling returns the next sibling node of the current node (read-only)
  11. Nodename: return the node name (read-only)
  12. Nodetype return node type (read-only)
  13. Nodetypedvalue stores node values (read/write)
  14. Nodevalue returns the node text (read/write)
  15. Ownerdocument returns the root document containing this node (read-only)
  16. Parentnode returns the parent node (read-only)
  17. Parsed: returns whether the node and Its subnodes have been parsed (read-only)
  18. Prefix returns the namespace prefix (read-only)
  19. Preservewhitespace specifies whether to retain white space (read/write)
  20. Previussibling returns the previous sibling node of this node (read-only)
  21. Text returns the text content of the node and its descendants (read/write)
  22. URL returns the URL of the recently loaded XML document (read-only)
  23. XML returns the XML Representation of the node and its descendants (read-only)

Method:

  1. Appendchild adds a new subnode to the current node, which is placed after the last subnode
  2. Clonenode returns the copy of the current node
  3. Createattribute
  4. Createcdatasection create CDATA segments that contain the given data
  5. Createcomment create a comment Node
  6. Createdocumentfragment create a documentfragment object
  7. Createelement: Create an element node
  8. Createentityreference: Create an entityreference object
  9. Createnode creates nodes of the given type, name, and namespace.
  10. Createporcessinginstruction
  11. Createtextnode: Create a text node that includes the given data
  12. Getelementsbytagname returns the set of elements with the specified name.
  13. Haschildnodes: returns whether the current node has subnodes.
  14. Insertbefore inserts a subnode before a specified Node
  15. Load import the XML document at the specified position
  16. Loadxml import the XML document of the specified string
  17. Removechild deletes a specified subnode from the subnode list.
  18. ReplaceChild replaces the specified child node from the child node list
  19. Save the XML file to the specified Node
  20. Selectnodes matches the specified node and returns the list of matched nodes.
  21. Selectsinglenode matches the specified node and returns the first matching node.
  22. Transformnode converts nodes and their descendants using the specified style table.
  23. Transformnodetoobject converts nodes and their descendants using the specified style table
<? PHP // read XML $ dom = new domdocument ('1. 0'); $ dom-> load ('data. XML '); $ em = $ dom-> getelementsbytagname ('videos'); // The outermost node $ em = $ em-> item (0 ); $ items = $ em-> getelementsbytagname ('video'); // node // if you do not need to read it, remove the following section to foreach ($ items as $) {foreach ($ A-> attributes as $ B) {// $ B-> nodevalue; node attribute value $ B-> nodename; node attribute name echo $ B-> nodename; echo ":"; echo $ B-> nodevalue; echo "<br/> ";}} // The following is a new $ T = $ dom-> createelement ('video '); // <Video $ T-> setattribute ('title', '1 '); // <video name = "data" $ T-> setattribute ('src', '2 '); // <video name = "data" src = "2" $ T-> setattribute ('img ', '1 '); // <video name = "data" IMG = "1" $ em-> appendchild ($ t ); // <video name = "data" IMG = "1"/> $ dom-> Save ('data. xml');?> The XML document at that time: <? XML version = "1.0"?> <Videos> <video IMG = "A" url = "1" Title = "1" nickname = "1" tag = "1" vid = "1" star = "1" /> <video IMG = "B" url = "2" Title = "2" nickname = "2" tag = "2" vid = "2" star = "2 "/> <video IMG = "C" url = "3" Title = "3" nickname = "3" tag = "3" vid = "3" star = "3"/> <video Title = "D" src = "2" IMG = "1"/> </videos> // the following file can be modified later. <? PHP $ Doc = new domdocument (); $ doc-> load ('data. xml'); // find the videos node $ root = $ doc-> getelementsbytagname ('videoos '); // The first videos node $ root = $ root-> item (0 ); // find the video node under the videos node $ userid = $ root-> getelementsbytagname ('video'); // traverse all video nodes foreach ($ userid as $ rootdata) {// traverse all attributes of each video node foreach ($ rootdata-> attributes as $ attrib) {$ attribname = $ attrib-> nodename; // nodename is the attribute name $ attribvalue = $ Attrib-> nodevalue; // nodevalue indicates the attribute content. // you can specify if ($ attribname = 'img ') as the node content whose attribute name is IP ') {// if ($ attribvalue = '1') {// modify the node content whose attribute is "IMG" and "IMG content is" 1 "to" image; $ rootdata-> setattribute ('img ', 'image'); $ doc-> Save ('data. xml') ;}}}?>

========================================================== ======================================

<? $ Xmldoc = new domdocument (); $ xmldoc-> load ("http: // localhost/XML. PHP "); $ x = $ xmldoc-> getelementsbytagname ('name'); For ($ I = 0; $ I <= $ X-> length-1; $ I ++) {If (strpos ($ X-> item ($ I)-> nodevalue, "fang ")! = False) {// determines whether the node content contains the character "fang", which can be used to search for ECHO $ X-> item ($ I) -> parentnode-> childnodes-> item (1)-> nodevalue ;}}?>

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.