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
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.
Load xml
$path=$_SERVER["DOCUMENT_ROOT"].'/books.xml'; $books=new DOMDocument(); $books->load($path);
Read/Traverse nodes and attributes
$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.' '; } echo '
'; }
Of course, you only want to read one attribute. you can use the item (index) method to read data by index.
echo $book->attributes->item(1)->nodeValue;
You can also use powerful xpath queries.
You can also use powerful xpath queries.
Modify attributes/nodes
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.
$book->setAttribute($attr->nodeName,strtoupper($attr->nodeValue));$attr->nodeValue=strtoupper($attr->nodeValue);
Add element/attribute
$ 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
$first=$bookElements->item(0); $first->removeAttribute('publisher'); $second=$bookElements->item(1); $second->parentNode->removeChild($second); $books->save($path);
For more php-related xml-related articles, refer to the Chinese PHP website!