Php operation xml

Source: Internet
Author: User

Data to be operated
Copy codeThe Code is 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: 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. <books> </books> Yes, <? Xml version = "1.0"?> Also, the <author> </author> label is, 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, elements, that is, elements are a subset of nodes. In short, they are considered as tags such as <xxx> </xxx>. There are usually 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
Copy codeThe Code is as follows:
$ Path = $ _ SERVER ["DOCUMENT_ROOT"]. '/books. xml ';
$ Books = new DOMDocument ();
$ Books-> load ($ path );

Read/traverse nodes and attributes
Copy codeThe Code is 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. 'handler .'';
}
Echo '<br/> ';
}



Of course, you only want to read one attribute. You can use the item (index) method to read data by index.
Copy codeThe Code is as follows:
Echo $ book-> attributes-> item (1)-> nodeValue;

You can also use powerful xpath queries.
Copy codeThe Code is as follows:
You can also use powerful xpath queries.

Modify attributes/nodes
Copy codeThe 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.
Copy codeThe Code is as follows:
$ Book-> setAttribute ($ attr-> nodeName, strtoupper ($ attr-> nodeValue ));
$ Attr-> nodeValue = strtoupper ($ attr-> nodeValue );

Add element/attribute
Copy codeThe 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
Copy codeThe 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.

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.