Php xml operation class domdocument attributes and Methods
Note that case sensitivity must not be incorrect.
Attribute:
- Attribute list of the attributes storage node (read-only)
- List of child nodes of the childnodes storage node (read-only)
- Datatype returns the Data Type of this node
- Definition defines nodes in DTD or XML mode (read-only)
- Doctype specifies the file type node (read-only)
- Documentelement returns the root element of the document (read/write)
- Firstchild returns the first child node of the current node (read-only)
- Implementation returns the xmldomimplementation object
- Lastchild returns the last child node of the current node (read-only)
- Nextsibling returns the next sibling node of the current node (read-only)
- Nodename: return the node name (read-only)
- Nodetype return node type (read-only)
- Nodetypedvalue stores node values (read/write)
- Nodevalue returns the node text (read/write)
- Ownerdocument returns the root document containing this node (read-only)
- Parentnode returns the parent node (read-only)
- Parsed: returns whether the node and Its subnodes have been parsed (read-only)
- Prefix returns the namespace prefix (read-only)
- Preservewhitespace specifies whether to retain white space (read/write)
- Previussibling returns the previous sibling node of this node (read-only)
- Text returns the text content of the node and its descendants (read/write)
- URL returns the URL of the recently loaded XML document (read-only)
- XML returns the XML Representation of the node and its descendants (read-only)
Method:
- Appendchild adds a new subnode to the current node, which is placed after the last subnode
- Clonenode returns the copy of the current node
- Createattribute
- Createcdatasection create CDATA segments that contain the given data
- Createcomment create a comment Node
- Createdocumentfragment create a documentfragment object
- Createelement: Create an element node
- Createentityreference: Create an entityreference object
- Createnode creates nodes of the given type, name, and namespace.
- Createporcessinginstruction
- Createtextnode: Create a text node that includes the given data
- Getelementsbytagname returns the set of elements with the specified name.
- Haschildnodes: returns whether the current node has subnodes.
- Insertbefore inserts a subnode before a specified Node
- Load import the XML document at the specified position
- Loadxml import the XML document of the specified string
- Removechild deletes a specified subnode from the subnode list.
- ReplaceChild replaces the specified child node from the child node list
- Save the XML file to the specified Node
- Selectnodes matches the specified node and returns the list of matched nodes.
- Selectsinglenode matches the specified node and returns the first matching node.
- Transformnode converts nodes and their descendants using the specified style table.
- 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:
- Attribute list of the attributes storage node (read-only)
- List of child nodes of the childnodes storage node (read-only)
- Datatype returns the Data Type of this node
- Definition defines nodes in DTD or XML mode (read-only)
- Doctype specifies the file type node (read-only)
- Documentelement returns the root element of the document (read/write)
- Firstchild returns the first child node of the current node (read-only)
- Implementation returns the xmldomimplementation object
- Lastchild returns the last child node of the current node (read-only)
- Nextsibling returns the next sibling node of the current node (read-only)
- Nodename: return the node name (read-only)
- Nodetype return node type (read-only)
- Nodetypedvalue stores node values (read/write)
- Nodevalue returns the node text (read/write)
- Ownerdocument returns the root document containing this node (read-only)
- Parentnode returns the parent node (read-only)
- Parsed: returns whether the node and Its subnodes have been parsed (read-only)
- Prefix returns the namespace prefix (read-only)
- Preservewhitespace specifies whether to retain white space (read/write)
- Previussibling returns the previous sibling node of this node (read-only)
- Text returns the text content of the node and its descendants (read/write)
- URL returns the URL of the recently loaded XML document (read-only)
- XML returns the XML Representation of the node and its descendants (read-only)
Method:
- Appendchild adds a new subnode to the current node, which is placed after the last subnode
- Clonenode returns the copy of the current node
- Createattribute
- Createcdatasection create CDATA segments that contain the given data
- Createcomment create a comment Node
- Createdocumentfragment create a documentfragment object
- Createelement: Create an element node
- Createentityreference: Create an entityreference object
- Createnode creates nodes of the given type, name, and namespace.
- Createporcessinginstruction
- Createtextnode: Create a text node that includes the given data
- Getelementsbytagname returns the set of elements with the specified name.
- Haschildnodes: returns whether the current node has subnodes.
- Insertbefore inserts a subnode before a specified Node
- Load import the XML document at the specified position
- Loadxml import the XML document of the specified string
- Removechild deletes a specified subnode from the subnode list.
- ReplaceChild replaces the specified child node from the child node list
- Save the XML file to the specified Node
- Selectnodes matches the specified node and returns the list of matched nodes.
- Selectsinglenode matches the specified node and returns the first matching node.
- Transformnode converts nodes and their descendants using the specified style table.
- 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 ;}}?>