PHP Operation xml_php Instance

Source: Internet
Author: User
Tags xpath

The data to manipulate

Copy Code code as follows:



<?xml version= "1.0"?>


<books>


<book name= "javascript:the defiitive Guide" publisher= "O ' Reilly Media, Inc." >


<author>david flanagan</author>


</book>


<book name= "PHP anf MySQL Web Development" publisher= "Perason Education" >


<author>luke welling</author>


<author>laura thomson</author>


</book>


<book name= "http:the defiitive Guide" publisher= "O ' Reilly Media, Inc." >


<author>david courley</author>


<author>brian totty</author>


</book>


</books>


Several basic concepts of XML
1, Node: nodes, the node in many programming languages where XML is processed, are a relatively broad concept in which elements, attributes, namespaces, annotations, text content, processing instructions, and the entire document belong to nodes, meaning that a small part of the XML document is a node, <books></books> is, <?xml version= "1.0"?> is also, name= "XXXX" is also,<author></author> label is, Even the author's name, David Flanagan, is a text node.
2, the element: Many programming languages have to the XML processing, the node is a very broad concept, because to unify the API, does not have too many methods to the node, but the element is the node is a subset of nodes, simply say is <xxx></xxx> such label is counted, There are usually many ways to manipulate elements.
3, attributes: This better understanding, in the <> of similar xx= "OO" and other things are attribute nodes
4, escape character: and HTML and so on, XML also has the language occupation symbol, want to use these special characters of time need to escape

DOMDocument objects
I use the DOMDocument object to manipulate XML, it feels more than simplexml science, of course, the first day to use PHP, is purely personal feeling. DOMDocument has several common properties and methods.


Loading XML

Copy Code code as follows:

$path =$_server["Document_root"]. ' /books.xml ';
$books =new DOMDocument ();
$books->load ($path);

Read/traverse nodes and attributes

Copy Code code as follows:

$bookElements = $books->getelementsbytagname (' book ');

foreach ($bookElements as $book) {
foreach ($book->attributes as $attr) {
Echo strtoupper ($attr->nodename). '-$attr->nodevalue. ' <br/> ';
}
echo "AUTHOR:";
foreach ($book->getelementsbytagname (' author ') as $author) {
echo $author->nodevalue. ' ';
}
Echo ' <br/><br/> ';
}










Of course for many properties, just want to read one, you can read by index method

Copy Code code as follows:

Echo $book->attributes->item (1)->nodevalue;



You can also use a powerful XPath query


Copy Code code as follows:

You can also use a powerful XPath query

modifying Properties/Nodes

Copy 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 access the property modification directly to its nodevalue changes, or you can use the SetAttribute method, and do not forget to save it with save.

Copy Code code as follows:

$book->setattribute ($attr->nodename,strtoupper ($attr->nodevalue));
$attr->nodevalue=strtoupper ($attr->nodevalue);

add element/attribute

Copy 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 Code code as follows:

$first = $bookElements->item (0);
$first->removeattribute (' publisher ');

$second = $bookElements->item (1);
$second->parentnode->removechild ($second);

$books->save ($path);













Beginner PHP article must have a lot of fallacies, I hope everyone criticized, common progress.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.