http://blog.csdn.net/morewindows/article/details/7102362
Reading and writing XML files with PHP_PHP tutorial
PHP can easily generate and read XML files. PHP mainly completes XML reading and writing operations through DOMDocument, DOMElement and DOMNodeList. Below is a brief explanation of how to use these classes.
1. Generate XML file
For an XML file as follows.
[html]
http://blog.csdn.net/morewindows/article/details/7102362
http://blog.csdn.net/morewindows/article/details/7102362
Let’s see how to generate it using PHP:
First create a new DOMDocument object and set the encoding format.
$dom = newDOMDocument('1.0', 'UTF-8');
$dom->formatOutput= true;
Create the
$rootelement =$dom->createElement("article");
$title =$dom->createElement("title", "PHP Access MySql Database - Elementary");
Then create a node with text content
$link =$dom->createElement("link","http://blog.csdn.net/morewindows/article/details/7102362");
You can also generate a node first and then add text content to it.
$link = $dom->createElement("link");
$linktext =$dom->createTextNode('http://blog.csdn.net/morewindows/article/details/7102362');
$link->appendChild($linktext);
Then add the
$rootelement->appendChild($title);
$rootelement->appendChild($link);
Finally, add the
$dom->appendChild($rootelement);
A complete XML is now generated. Then regenerate the entire XML,
echo $dom->saveXML() ;
saveXML() can also input only part of the XML text. For example, echo $dom->saveXML($link); will only output the node: http://blog.csdn. net/morewindows/article/details/7102362
The following is a complete example of outputting data content to an XML file in PHP. This example will output a PHP array to an XML file.
[php]
//Output the array into an XML file
// by MoreWindows( http://blog.csdn.net/MoreWindows )
$article_array = array(
"First article" => array(
"title"=>"Access MySql Database with PHP - Elementary",
"link"=>"http://blog.csdn.net/morewindows/article/details/7102362"
),
"Part 2" => array(
"title"=>"PHP Access MySql Database Intermediate Smarty Technology",
"link"=>"http://blog.csdn.net/morewindows/article/details/7094642"
),
"Part 3" => array(
"title"=>"PHP Access MySql Database Advanced AJAX Technology",
"link"=>"http://blog.csdn.net/morewindows/article/details/7086524"
),
);
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$rootelement = $dom->createElement("MoreWindows");
foreach ($article_array as $key=>$value)
{
$article = $dom->createElement("article", $key);
$title = $dom->createElement("title", $value['title']);
$link = $dom->createElement("link", $value['link']);
$article->appendChild($title);
$article->appendChild($link);
$rootelement->appendChild($article);
}
$dom->appendChild($rootelement);
$filename = "D:\test.xml";
echo 'XML file size' . $dom->save($filename) . 'Bytes';
?>
//Output the array to an XML file
// by MoreWindows( http://blog.csdn.net/MoreWindows )
$article_array = array(
"First article" => array(
"title"=>"Access MySql database with PHP - Elementary",
"link"=>"http://blog.csdn.net/morewindows/article/details/7102362"
),
"Part 2" => array(
"title"=>"PHP Access MySql Database Intermediate Smarty Technology",
"link"=>"http://blog.csdn.net/morewindows/article/details/7094642"
),
"Part 3" => array(
"title"=>"PHP Access MySql Database Advanced AJAX Technology",
"link"=>"http://blog.csdn.net/morewindows/article/details/7086524"
),
);
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$rootelement = $dom->createElement("MoreWindows");
foreach ($article_array as $key=>$value)
{
$article = $dom->createElement("article", $key);
$title = $dom->createElement("title", $value['title']);
$link = $dom->createElement("link", $value['link']);
$article->appendChild($title);
$article->appendChild($link);
$rootelement->appendChild($article);
}
$dom->appendChild($rootelement);
$filename = "D:\test.xml";
echo 'XML file size' . $dom->save($filename) . 'Bytes';
?>
Running this PHP will generate the test.xml file on the D drive (Win7 + XAMPP + IE9.0 test passed)
2. Read XML file
Take reading the D:\test.xml generated in the previous article as an example:
[php]
//读取XML文件
// by MoreWindows( http://blog.csdn.net/MoreWindows )
$filename = "D:\test.xml";
$article_array = array();
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->load($filename);
//得到
$articles = $dom->getElementsByTagName("article");
echo '
foreach ($articles as $article)
{
$id = $article->getElementsByTagName("id")->item(0)->nodeValue;
$title = $article->getElementsByTagName("title")->item(0)->nodeValue;
$link = $article->getElementsByTagName("link")->item(0)->nodeValue;
$article_array[$id] = array('title'=>$title, 'link'=>$link);
}
//输出结果
echo ""; <br>
var_dump($article_array); <br>
echo "
";
?>
//读取XML文件
// by MoreWindows( http://blog.csdn.net/MoreWindows )
$filename = "D:\test.xml";
$article_array = array();
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->load($filename);
//得到
$articles = $dom->getElementsByTagName("article");
echo '
foreach ($articles as $article)
{
$id = $article->getElementsByTagName("id")->item(0)->nodeValue;
$title = $article->getElementsByTagName("title")->item(0)->nodeValue;
$link = $article->getElementsByTagName("link")->item(0)->nodeValue;
$article_array[$id] = array('title'=>$title, 'link'=>$link);
}
//输出结果
echo "
";<br> var_dump($article_array);<br> echo "";
?>
运行结果如下:
摘自 MoreWindows

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
