1 simplexml Introduction
There are two traditional approaches to processing XML files: Sax and Dom. Based on the event trigger mechanism,
Scan the XML file once to complete the processing. Dom constructs the entire XML file into a DOM
To process the DOM tree. The two methods have their own advantages and disadvantages. The processing logic of Sax is relatively abstract,
The Dom processing process is cumbersome and is not suitable for beginners.
PhP5 introduces a new set of XML processing functions, namely simplexml. Simplexml is actually small.
Coincidentally, only a few method functions are provided, but the XML file processing function is very powerful.
Very simple.
First, it provides simple functions that can be directly constructed from XML documents, strings, or DOM objects.
Simplexmlelement object. Secondly, simplexmlelement provides a simple method for attributes and subsections.
Point, and XPath operations; however, the simplest thing about simplexml is that it provides the attributes and
The method used by the object iterator to perform node operations. This processing method greatly improves the processing of XML documents with PHP.
.
2 simplexml getting started example
Below we use some small Code Snippet to learn a little about the power and conciseness of simplexml. For convenience,
We use a messages. xml file that contains the following XML code:
Messages. xml
Copy code The Code is as follows: <? XML version = '1. 0' standalone = 'Yes'?>
<Messages>
<MSG id = '1'>
<Title> This is Title </title>
<Content> here is content </content>
<Time> 21:50:23 </time>
<Reply id = '11'> Reply 1 </reply>
<Reply id = '12'> Reply 2 </reply>
</MSG>
</Messages>
This is an XML document that stores message information, including the property ID, subnode title, content, and time.
And a number of response information for it. Each response includes the attribute ID and response content.
The process and method for processing and outputting the content of this XML document using simplexml are as follows.
(1) construct simplexmlelement object
code snippet
$ xml = simplexml_load_file ('messages. XML ');
If the XML segment has been read into a string $ messages, you can use the following statement:
code snippet
$ xml = simplexml_load_string ('messages. XML ');
(2) output the title of Message 1
code snippet
// you can access the subnode using attributes, you can directly obtain the node content by using the label name of the node
echo $ XML-> MSG-> title;
(3) output The First Response Message in Message 1
code snippet
// multiple nodes with the same name at the same level are automatically used as arrays, you can use index subscript to access its content
echo $ XML-> MSG-> reply [0];
(4) ID of the Output Message
code snippet
// The node attributes and values are encapsulated into the keys and values of the associated array
echo $ XML-> MSG ['id '];
(5) output the ID of the second response
code snippet
// It is a two-dimensional array, with the first dimension representing the node, 2d representation of attributes
echo $ XML-> MSG-> reply [1] ['id'];
(6) output all response IDs in sequence
code snippet
// use foreach to traverse nodes with the same name
foreach ($ XML-> MSG-> reply as $ reply) {
echo $ reply ['id'];
}< br> (7) use XPath to retrieve all Response Information
code snippets
// The XPath method for direct retrieval and locating (// indicating any depth)
foreach ($ XML-> XPath ('// reply') as $ reply) {
echo $ reply. '
';
}
(8) traverse Message 1 All subnodes
Code snippet
// Obtain all child nodes using the children Method
Foreach ($ XML-> MSG-> Children () as $ field ){
Echo $ field. '<br> ';
}
(9) reset the release time of Message 1.
Code snippet
// Directly set attributes
$ XML-> MSG-> time = '2017-03-21 00:53:12 ';
(10) set the ID attribute of Reply 2
Code snippet
// Set the value of the Management Array
$ XML-> MSG-> reply [1] ['id'] = '2013 ';
(11) Add a field describing the message author.
Code snippet
// Directly set attributes
$ XML-> MSG-> author = 'hangsan ';
(12) Save the message author as an attribute
Code snippet
// Set the key of the associated array
$ XML-> MSG ['autor'] = 'hangsan ';
(13) Save the object to the file again
Code snippet
// Save
$ XML-> asxml ('messagesnew. xml ');
We can see how simple simplexml is!
3. Example: Data Interaction Between XML files and databases
The following provides a relatively complete instance to query the message information from the MySQL database and save it
As shown in the preceding example. The message and reply information are stored in two tables independently, using the MySQL function package
It can be easily implemented as follows:
The Code is as follows: Copy code The Code is as follows: <? PHP
// Cong work atwed Mar 20 19:59:04 CST 2008
// Save the data from the MySQL database to the XML file
// You can use the following methods to construct the initial simplexmlelement object:
// 1. construct from the DOM object
// $ Dom = new domdocument ();
// $ Dom-> loadxml ("<rows> </rows> ");
// $ Xml = simplexml_import_dom ($ DOM );
// 2. construct from an XML file containing only the root tag
// $ Xml = simplexml_load_file ('messages. xml ');
// 3. directly write the root tag string to construct
// $ Xml = simplexml_load_string ("<messages> </messages> ");
// 4. Use the constructor of the simplexmlelement class to construct
$ Xml = new simplexmlelement ('<messages> </messages> ');
// Connect to the database
Mysql_connect ('localhost', 'root', 'root ');
Mysql_select_db ('test ');
Mysql_query ('set names utf8 ');
// Query a message
$ Rs = mysql_query ("select * from messages ");
$ I = 0; // index the subscript with an array of multiple messages
While ($ ROW = mysql_fetch_assoc ($ RS )){
$ XML-> message [$ I] = '';//... ... ... ... ... ... ... ... ... ... ... ... ①
$ XML-> message [$ I] ['id'] = $ row ['id'];
$ XML-> message [$ I]-> Title = $ row ['title'];
$ XML-> message [$ I]-> content = $ row ['content'];
$ XML-> message [$ I]-> time = $ row ['time'];
// Query the reply information of a message based on the message id.
$ Rsreply = mysql_query ("select * from replies where mid = {$ row ['id']}");
$ J = 0; // index subscript used for Multiple replies
While ($ rowreply = mysql_fetch_assoc ($ rsreply )){
$ XML-> message [$ I]-> reply [$ J] = $ rowreply ['reply'];
$ XML-> message [$ I]-> reply [$ J] ['id'] = $ rowreply ['id'];
$ J ++;
}
$ I ++;
}
$ XML-> asxml ('messages. xml ');
?>
The only thing worth mentioning in the above Code is the line marked as ①. When we want to add a new
When adding a node or attribute, you must ensure that its parent node exists. Otherwise, a fatal error is reported and the message is:
Objects used as arrays in post/PRE increment/decrement must return values by reference. Hope everyone
Do not be confused by the prompt of the migration cloud. I believe that readers can write a code from an XML file to MySQL through their understanding of the above Code.