else if($e->nodeType == XML_ELEMENT_NODE) //If the child node is a node object, call the function processing
The running results are as follows.
The above example uses a recursive method to process XML data and realizes the function of outputting all text tags in XML data.
The basic operations of XML have been introduced before. This section will take designing an XML guestbook as an example to explain in detail how to realize the interaction between PHP and XML data in practical applications.
XML files are used to store XML data, which are the messages in the guestbook. Here, for each message, the XML data mainly includes three contents: message title, message author's name, and message content. Therefore, the format of the XML file is designed as follows.
The message submission page consists of two pages. One is an HTML file for a form that allows visitors to write messages, and the other is a PHP script used to process visitor input. The HTML code for the form is shown below.
对于用来处理用户输入的 PHP脚本,其基本逻辑是首先创建一个 DOM对象,然后读取 XML文件中的 XML数据,接下来在 XML对象上创建新的节点并将用户的输入储存起来,最后将 XML数据输出到原来的 XML文件中。具体实现代码如下所示。
$guestbook = new DomDocument(); //创建一个新的 DOM对象
$guestbook->load('DB/guestbook.xml'); //读取 XML数据
$threads = $guestbook->documentElement; //获得 XML结构的根
//创建一个新 thread节点
$thread = $guestbook->createElement('thread');
$threads->appendChild($thread);
//在新的 thread节点上创建 title标签
$title = $guestbook->createElement('title');
$title->appendChild($guestbook->createTextNode($_POST['title']));
$thread->appendChild($title);
//Create author tag on the new thread node
$author = $guestbook->createElement('author');
$author->appendChild($guestbook->createTextNode($_POST['author']));
$thread->appendChild($author);
//Create content tag on the new thread node
$content = $guestbook->createElement('content');
$content->appendChild($guestbook->createTextNode($_POST['content']));
$thread->appendChild($content);
//Write XML data to file
$fp = fopen("DB/guestbook.xml", "w");
if(fwrite($fp, $guestbook->saveXML()))
echo “Message submitted successfully”;
else
echo “Message submission failed”;
fclose($fp);
?>
Run the above HTML file in the browser and fill in the appropriate message content, as shown in Figure 2.
Figure 2 New message posting interface
After clicking the [Submit] button, the content in the XML file is as follows.
You can see that the message has been stored in the XML file.
4.3 Writing the display page
The display page can be easily implemented using the SimpleXML component introduced earlier. The specific implementation code is as follows.
Copy the code The code is as follows:
//Open the XML file used to store messages
$guestbook = simplexml_load_file('DB/guestbook.xml');
foreach($guestbook->thread as $th) //Loop to read each thread tag in the XML data
{
echo “Title:”.$th->title.”
”;
echo “Author:”.$th->author.”
”;
echo “
Content:”.$th->content.”
”;
echo “
”;
}
?>
View the running results in the browser as shown in Figure 3.
Articles you may be interested in:
PHP code for reading XML values (recommended)
php reads xml example code
Several ways to write and read XML with PHP
Sharing 4 ways to generate XML files in PHP
Implementation code for reading and writing XML DOM with PHP
Instructions for using the simplexml_load_string function in PHP
php xml introductory learning materials
PHP operates XML as a database class
php generates xml simple example code
The implementation code of combining php with XML, XSLT and Mysql
The XML generated by PHP is obtained by FLASH as the ultimate solution to garbled characters
Introduction to PHP reading xml method
http://www.bkjia.com/PHPjc/477139.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477139.htmlTechArticleXML is a popular semi-structured file format that stores data in a database-like format. In practical applications, some simple and low-security data often use XML files...