Step-by-Step learning of php xml operations: XML concepts, DOMDocument objects, loading xml, reading and traversing nodes and attributes, modifying attribute nodes, adding element attributes, and deleting attribute nodes, learn the above to operate XML smoothly. Data to be operated
The Code is as follows:
David Flanagan
Luke Welling
Laura Thomson
David Courley
Brian Totty
Several Basic concepts of XML
1. Node: the Node used to process XML in many programming languages. A Node is a broad concept. In XML, elements, attributes, namespaces, comments, and text content, the Processing Command and the entire document belong to the node. That is to say, each small part of the XML document is a node, Yes, Also, name = "XXXX,Tags are, and even the author's name David Flanagan is a text node.
2. element: many programming languages have XML processing. nodes are a very broad concept. To unify APIs, there will not be too many methods for nodes, element is a subset of nodes. Such a label generally has many operation methods for elements.
3. attributes: This is easy to understand. In the <> content similar to XX = "OO" is an attribute node.
4. escape characters: similar to HTML, xml also has symbols used by the language. To use these special characters, escape them.
DOMDocument object
I am using a DOMDocument object to operate xml. I feel that it is more scientific than simpleXml. Of course, the first day I used php is my personal experience. DOMDocument has several common attributes and methods.
Load xml
The Code is as follows:
$ Path = $ _ SERVER ["DOCUMENT_ROOT"]. '/books. xml ';
$ Books = new DOMDocument ();
$ Books-> load ($ path );
Read/traverse nodes and attributes
The Code is as follows:
$ BookElements = $ books-> getElementsByTagName ('book ');
Foreach ($ bookElements as $ book ){
Foreach ($ book-> attributes as $ attr ){
Echo strtoupper ($ attr-> nodeName). '--'. $ attr-> nodeValue .'
';
}
Echo "AUTHOR :";
Foreach ($ book-> getElementsByTagName ('author') as $ author ){
Echo $ author-> nodeValue. 'handler .'';
}
Echo'
';
}
Of course, you only want to read one attribute. You can use the item (index) method to read data by index.
The Code is as follows:
Echo $ book-> attributes-> item (1)-> nodeValue;
You can also use powerful xpath queries.
The Code is as follows:
You can also use powerful xpath queries.
Modify attributes/nodes
The Code is as follows:
Foreach ($ bookElements as $ book ){
Foreach ($ book-> attributes as $ attr ){
# $ Book-> setAttribute ($ attr-> nodeName, strtoupper ($ attr-> nodeValue ));
$ Attr-> nodeValue = strtoupper ($ attr-> nodeValue );
}
Echo "AUTHOR :";
Foreach ($ book-> getElementsByTagName ('author') as $ author ){
$ Author-> nodeValue = strtoupper ($ author-> nodeValue );
}
}
$ Books-> save ($ path );
You can directly access the nodeValue modification of the attribute modification, or use the setAttribute method. After the modification is complete, do not forget to save it with save.
The Code is as follows:
$ Book-> setAttribute ($ attr-> nodeName, strtoupper ($ attr-> nodeValue ));
$ Attr-> nodeValue = strtoupper ($ attr-> nodeValue );
Add element/attribute
The Code is as follows:
$ NewBook = $ books-> createElement ('book'); # create a new element
$ NewBook-> setAttribute ('name', 'php Objects, Patterns, and Practice '); # create a new attribute. method 1
$ Publisher = $ books-> createAttribute ('her her '); # create a new attribute. Method 2:
$ Publisher-> nodeValue = 'apress L. P ';
$ NewBook-> appendChild ($ publisher); # Add the attribute to the element.
$ Author = $ books-> createElement ('author'); # create a child element
$ Author-> nodeValue = 'Matt Zandstra ';
$ NewBook-> appendChild ($ author); # Add the child element to the parent element.
$ Books-> documentElement-> appendChild ($ newBook); # Add the entire Node
$ Books-> save ($ path );
Delete attribute/node
The Code is as follows:
$ First = $ bookElements-> item (0 );
$ First-> removeAttribute ('her her ');
$ Second = $ bookElements-> item (1 );
$ Second-> parentNode-> removeChild ($ second );
$ Books-> save ($ path );
Php beginners certainly have many mistakes. I hope you can criticize and correct them and make common progress.