PHP read/write XML file _ PHP Tutorial

Source: Internet
Author: User
PHP reads and writes XML files. PHP can easily generate and read XML files. PHP mainly reads and writes XML through DOMDocument, DOMElement, and DOMNodeList. The following describes how to use PHP to easily generate and read XML files. PHP mainly reads and writes XML through DOMDocument, DOMElement, and DOMNodeList. The following describes how to use these classes.

1. generate an XML file
For the following XML file.

[Html]

PHP accessing MySql database
Http://blog.csdn.net/morewindows/article/details/7102362 </link>



PHP accessing MySql database
Http://blog.csdn.net/morewindows/article/details/7102362 </link>

Let's take a look at how to use PHP to generate:

First, create a new DOMDocument object and set the encoding format.

$ Dom = newDOMDocument ('1. 0', 'utf-8 ');

$ Dom-> formatOutput = true;

Create a node andNode </p>

$ Rootelement = $ dom-> createElement ("article ");

$ Title = $ dom-> createElement ("title", "PHP accessing MySql database ");

Then create Node

$ Link = $ dom-> createElement ("link", "http://blog.csdn.net/morewindows/article/details/7102362 ");

Or Mr. Cheng Add text content to the node.

$ Link = $ dom-> createElement ("link ");

$ Linktext = $ dom-> createTextNode ('http: // blog.csdn.net/morewindows/article/details/7102362 ');

$ Link-> appendChild ($ linktext );

ThenAnd Add a node to the node

$ Rootelement-> appendChild ($ title );

$ Rootelement-> appendChild ($ link );

Add the node to the DOMDocument object,

$ Dom-> appendChild ($ rootelement );

The complete XML file is generated. And then the entire XML,

Echo $ dom-> saveXML ();

SaveXML () can also input only some XML text, such as echo $ dom-> saveXML ($ link); only Node: Http://blog.csdn.net/morewindows/article/details/7102362 </link>

The following example shows how to output the complete PHP Data content to an XML file. In this example, a PHP array is output to an XML file.

[Php] // Output the array to the XML file
// By MoreWindows (http://blog.csdn.net/MoreWindows)
$ Article_array = array (
"Article 1" => array (
"Title" => "accessing MySql database using PHP ",
"Link" => "http://blog.csdn.net/morewindows/article/details/7102362"
),
"Article 2" => array (
"Title" => "accessing MySql database using PHP intermediate Smarty technology ",
"Link" => "http://blog.csdn.net/morewindows/article/details/7094642"
),
"Article 3" => array (
"Title" => "PHP advanced AJAX technology for MySql database access ",
"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). 'Byte ';
?>
// Output the array to the XML file
// By MoreWindows (http://blog.csdn.net/MoreWindows)
$ Article_array = array (
"Article 1" => array (
"Title" => "accessing MySql database using PHP ",
"Link" => "http://blog.csdn.net/morewindows/article/details/7102362"
),
"Article 2" => array (
"Title" => "accessing MySql database using PHP intermediate Smarty technology ",
"Link" => "http://blog.csdn.net/morewindows/article/details/7094642"
),
"Article 3" => array (
"Title" => "PHP advanced AJAX technology for MySql database access ",
"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). 'Byte ';
?>
Run PHP to generate the test. xml file on drive D (Win7 + XAMPP + IE9.0 passed the test)

II. read XML files
Take the generated D: \ test. xml as an example:

[Php] // Read the 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 );

// Obtain the node
$ Articles = $ dom-> getElementsByTagName ("article ");
Echo 'number of knot'. $ 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"

"; 
var_dump($article_array);
echo "
";
?>
// Read the 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 );

// Obtain the node
$ Articles = $ dom-> getElementsByTagName ("article ");
Echo 'number of knot'. $ 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"

";
var_dump($article_array);
echo "
";
?>
The running result is as follows:



From MoreWindows

Bytes. PHP mainly reads and writes XML through DOMDocument, DOMElement, and DOMNodeList. The following briefly describes how to use these...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.