The data to manipulate
Copy CodeThe code is as follows:
David Flanagan
Luke Welling
Laura Thomson
David Courley
Brian Totty
Several basic concepts of XML
1, Node: node is a lot of programming language in the processing of XML node, nodes is a relatively broad concept, in XML elements, attributes, namespaces, annotations, text content, processing instructions, and the entire document belongs to the node, that is, the XML document in each of a small number of separate nodes, Yes, also, name= "XXXX" is also, tag is, even the author's name David Flanagan is a text node.
2, elements: Many programming languages have XML processing, node is a very broad concept, because to unify the API, there is no too many methods for the node, and the element is a subset of the node, which is simply said to be such a label to calculate, There are generally many ways to manipulate elements.
3. Properties: This is a good understanding, in <> similar to xx= "OO" and so on are attribute nodes
4, the escape character: and HTML, and so on, XML also has language occupied symbols, want to use these special characters need to escape
DOMDocument Object
I use the DOMDocument object to manipulate XML, feel more scientific than simplexml, of course, the first day of use of PHP, purely personal feeling. DOMDocument has several common properties and methods.
Load XML
Copy the Code code as follows:
$path =$_server["Document_root"]. ' /books.xml ';
$books =new DOMDocument ();
$books->load ($path);
Read/Traverse Nodes and properties
Copy the Code code 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. ' ';
}
Echo '
';
}
Of course for many properties, just want to read one, can be indexed by the item index method to read
Copy the Code code as follows:
Echo $book->attributes->item (1)->nodevalue;
You can also query through powerful XPath
Copy the Code code as follows:
You can also query through powerful XPath
modifying Properties/Nodes
Copy the Code code 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 its NodeValue changes to property modifications, or you can use the SetAttribute method, and don't forget to save with save.
Copy the Code code as follows:
$book->setattribute ($attr->nodename,strtoupper ($attr->nodevalue));
$attr->nodevalue=strtoupper ($attr->nodevalue);
add element/attribute
Copy the Code code as follows:
$newBook = $books->createelement (' book '); #创建新元素
$newBook->setattribute (' name ', ' PHP Objects, Patterns, and Practice '); #创建新属性, method one
$publisher = $books->createattribute (' publisher '); #创建新属性, method two
$publisher->nodevalue= ' Apress l.p ';
$newBook->appendchild ($publisher); #把属性添加到元素上
$author = $books->createelement (' author '); #创建子元素
$author->nodevalue= ' Matt Zandstra ';
$newBook->appendchild ($author); #把子元素添加到父元素上
$books->documentelement->appendchild ($newBook); #添加整个节点
$books->save ($path);
Delete attribute/node
Copy the Code code as follows:
$first = $bookElements->item (0);
$first->removeattribute (' publisher ');
$second = $bookElements->item (1);
$second->parentnode->removechild ($second);
$books->save ($path);
Beginners PHP article must have a lot of errors, I hope you criticize correct, common progress.