http://blog.csdn.net/morewindows/article/details/7102362
Home >Backend Development >PHP Tutorial >Tips for reading and writing XML files with PHP_PHP tutorial
Commonly 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]
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 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';
?>
//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]
//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
$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);
}
//Output results
echo ""; <br>
var_dump($article_array); <br>
echo "
";
?>
//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
$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);
}
//Output results
echo "
";<br> var_dump($article_array);<br> echo "";