Data to be manipulated
Copy code The code is as follows:
David Flanagan
Luke Welling
Laura Thomson
David Courley
Several basic concepts of XML
1. Node: Node is the Node used in many programming languages when processing XML. Node is a relatively broad concept. In XML, elements, attributes, namespaces, comments, and text content , processing instructions, and the entire document belong to nodes, that is to say, each independent small part of the XML document is a node, is, So is name="XXXX", the tag is, and even the author's name David Flanagan is a text node.
2. Element: Many programming languages have XML processing. Node is a very broad concept. Because of the need to unify the API, there will not be too many methods for nodes, and Element is a subset of nodes. To put it simply, tags like generally have many operation methods for elements. 3. Attributes: This is easier to understand. Things like XX="OO" in <> are all attribute nodes 4. Escape characters: Similar to HTML, xml also has language occupation Symbols, you need to escape these special characters when you want to use them
DOMDocument object
I use the DOMDocument object to operate xml. I feel that it is more scientific to use than simpleXml. Of course, I used it on the first day php, purely personal feelings. DOMDocument has several commonly used properties and methods.
Load xml
Copy code
The code is as follows:
$path=$_SERVER["DOCUMENT_ROOT"].'/ books.xml'; $books=new DOMDocument(); $books->load($path);
Read/traverse nodes and attributes
Copy code
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->nodeValue.' ;
Of course, for many attributes, if you only want to read one, you can read by index through the item(index) method
Copy the code
The code is as follows:
echo $book->attributes->item(1)->nodeValue;
You can also query through powerful xpath
Copy the code The code is as follows:
You can also query through the powerful xpath
Modify attributes/nodes
Copy code The code is as follows:
foreach($bookElements as $book){
foreach ($book->attributes as $attr) {
setAttribute($attr->nodeName,strtoupper ($attr->nodeValue));
$attr->nodeValue=strtoupper($attr->nodeValue);
}
echo "AUTHOR: ";
foreach ($book ->getElementsByTagName('author') as $author) {
} $books->save($path);
To modify attributes, you can directly access its nodeValue changes, or you can use the setAttribute method. Don’t forget to use save after making changes.
Copy code The code is as follows:
$book->setAttribute($attr->nodeName,strtoupper($attr ->nodeValue));
$attr->nodeValue=strtoupper($attr->nodeValue);
Add elements/attributes
Copy code 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 one
$publisher=$books->createAttribute('publisher');#Create a new attribute, method two $publisher->nodeValue='Apress L.P';
$newBook-> appendChild($publisher); #Add attributes to elements
$author=$books->createElement('author');#Create child element
$author->nodeValue='Matt Zandstra';
$newBook->appendChild($author) ;#Add child elements to parent elements
$books->documentElement->appendChild($newBook);#Add the entire node
$books->save($path);
Delete attributes/nodes
Copy code The code is as follows:
$first=$bookElements->item(0 );
$first->removeAttribute('publisher');
$second=$bookElements->item(1); $second->parentNode->removeChild($second);
$books->save($path);
There must be many fallacies in beginner PHP articles, I hope everyone can criticize and correct them , make progress together.
http://www.bkjia.com/PHPjc/824929.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824929.htmlTechArticleThe data copy code to be operated on is as follows: ?xml version="1.0"? books book name="JavaScript: The Defiitive Guide" publisher="O'Reilly Media, Inc." authorDavid Flanagan/author /bo...