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);
Copy code
php can easily generate and read xml files.
XML reading and writing operations are mainly completed through DOMDocument, DOMElement and DOMNodeList.
The following is an introduction to how to use these classes for your reference.
One. Generate XML file
For an XML file as follows.
PHP access to mysql database primary article
< ;article>
Preliminary article on accessing MySql database with PHP
http://blog.csdn.net/morewindows/article/details/7102362
Copy code Let’s see how to generate it with PHP:
First create a new DOMDocument object and set the encoding format.
$dom = newDOMDocument('1.0', 'UTF-8'); $dom->formatOutput= true;
Copy the code
Create nodes and
nodes
$rootelement =$dom->createElement("article");
$title =$dom->createElement("title", "PHP Access MySql Database Elementary Article");
Copy Code
and then create a node with text content
$link =$dom->createElement("link","http://blog.csdn.net/morewindows/article/details/7102362");
Copy code
also You can generate the 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);
Copy the code
and then add the
and nodes to the nodes
$rootelement->appendChild($title);
$rootelement->appendChild($link);
Copy the code
Finally add the node to the DOMDocument object,
$dom->appendChild($rootelement);
Copy the code
A complete XML is generated. Then regenerate the entire XML,
saveXML() can also input only part of the XML text, such as echo $dom->saveXML($link); it 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.
//Output the array into an XML file // by MoreWindows( http://blog.csdn.net/MoreWindows ) $article_array = array( "First Article" => array(
"title"=>"Initial PHP Access to MySql Database",
"link"=>"http://blog.csdn.net/morewindows/article/details/7102362"
),
"Part 2" => array(
"title"=>"Intermediate article Smarty technology for PHP access to MySql database",
"link"=>"http://blog.csdn.net/morewindows/article /details/7094642"
),
"Part 3" => array(
"title"=>"Advanced AJAX Technology for PHP to Access MySql Database",
"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';
?>
Copy code
#----- ---------------
//Output the array into an XML file // by MoreWindows( http://blog.csdn.net/MoreWindows ) $article_array = array( "First Article" => array(
"title"=>"Initial PHP Access to MySql Database",
"link"=>"http://blog.csdn.net/morewindows/article/details/7102362"
),
"Part 2" => array(
"title"=>"Intermediate article Smarty technology for PHP access to MySql database",
"link"=>"http://blog.csdn.net/morewindows/article /details/7094642"
),
"Part 3" => array(
"title"=>"Advanced AJAX Technology for PHP to Access MySql Database",
"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';
?>
Copy code
Running this PHP will generate the test.xml file on the D drive (Win7 + XAMPP + IE9.0 test passed)
II. Read XML files
Take reading the D:test.xml generated in the previous article as an example:
//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);< ;/p>
//Getnode
$articles = $dom->getElementsByTagName("article");
echo '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 result
echo ""; </li>
<li>var_dump($article_array);</li>
<li>echo " ";
?>
Copy code
#-------------------------- ---
//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);< ;/p>
//Getnode
$articles = $dom->getElementsByTagName("article");
echo '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 result
echo ""; </li>
<li>var_dump($article_array);</li>
<li>echo " ";
?>
Copy code