Tips for reading and writing XML files with PHP_PHP tutorial
Jul 20, 2016 am 11:18 AMCommonly used lines are as follows:
header("content-type:text/html; charset=utf-8"); //Specify PHP to use UTF-8 encoding
$xml = simplexml_load_file("example.xml"); //Read xml file
$newxml = $xml->asXML(); //Standardize $xml
$fp = fopen("newxml.xml", "w"); //New xml file
fwrite($fp, $newxml); //Write -------xml file
fclose($fp);
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] <?xml version="1.0" encoding="UTF-8"?>
<article>
<title>PHP Access MySql Database Basics</title>
<link>http://blog.csdn.net/morewindows/article/details/7102362</link>
</article>
<?xml version="1.0" encoding="UTF-8"?>
<article>
<title>PHP Access MySql Database Basics</title>
<link>http://blog.csdn.net/morewindows/article/details/7102362</link>
</article>
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 <article> node and the <title> node
$rootelement =$dom->createElement("article");
$title =$dom->createElement("title", "PHP Access MySql Database - Elementary");
Then create a <link> node with text content
$link =$dom->createElement("link","http://blog.csdn.net/morewindows/article/details/7102362");
You can also generate a <link> 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 <title> and <link> nodes to the <article> node
$rootelement->appendChild($title);
$rootelement->appendChild($link);
Finally, add the <article> node to the DOMDocument object,
$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 <link> node: <link>http://blog.csdn. net/morewindows/article/details/7102362</link>
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] <?php
//Output the array to an XML file
// by MoreWindows( http://blog.csdn.net/MoreWindows )
$article_array = array(
"First article" => array(
"title"=>"PHP Access MySql Database Beginner",
"link"=>"http://blog.csdn.net/morewindows/article/details/7102362"
),
"Second article" => 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';
?>
<?php
//Output the array to an XML file
// by MoreWindows( http://blog.csdn.net/MoreWindows )
$article_array = array(
"First article" => array(
"title"=>"PHP Access MySql Database Beginner",
"link"=>"http://blog.csdn.net/morewindows/article/details/7102362"
),
"Second article" => 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] <?php
//Read XML file
// by MoreWindows( http://blog.csdn.net/MoreWindows )
$filename = "D:test.xml";
$article_array = array();
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->load($filename);
//Get the <article> node
$articles = $dom->getElementsByTagName("article");
echo '<article> Number of nodes' . $articles->length;
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);
}
//Output results
echo "<pre>";
var_dump($article_array);
echo "</pre>";
?>
<?php
//Read XML file
// by MoreWindows( http://blog.csdn.net/MoreWindows )
$filename = "D:test.xml";
$article_array = array();
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->load($filename);
//Get<article>node
$articles = $dom->getElementsByTagName("article");
echo '<article> Number of nodes' . $articles->length;
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);
}
//Output results
echo "<pre>";
var_dump($article_array);
echo "</pre>";
?>

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

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

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian
