PHP Read and write XML file tips

Source: Internet
Author: User
Tags foreach file size mysql database

The following lines are commonly used:

Header ("content-type:text/html; Charset=utf-8 "); Specifies that PHP uses UTF-8 encoding
$xml = simplexml_load_file ("Example.xml"); reading XML files
$newxml = $xml->asxml (); Standardized $xml
$fp = fopen ("Newxml.xml", "w"); New XML file
Fwrite ($fp, $newxml); Write-------XML file
Fclose ($FP);

PHP makes it easy to generate and read XML files. PHP mainly through the DOMDocument, DomElement and domnodelist to complete the XML read and write operations. Here's a brief description of how to use these classes.

A Generate XML file
For one of the following XML files.

[HTML] <?xml version= "1.0" encoding= "UTF-8"?>
<article>
<title>php Access MySQL database primary article </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 primary article </title>
<link>http://blog.csdn.net/morewindows/article/details/7102362</link>
</article>
Let's take a look at how PHP is used to generate:

First the new DOMDocument object and set the encoding format.

$dom = newdomdocument (' 1.0 ', ' UTF-8 ');

$dom->formatoutput= true;

Re-create <article> nodes and <title> nodes.

$rootelement = $dom->createelement ("article");

$title = $dom->createelement ("title", "PHP access to MySQL database primary article");

Then create a <link> node with text content

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

You can also build <link> nodes and then add text content to them.

$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, the <article> node is added to the DOMDocument object.

$dom->appendchild ($rootelement);

Such a complete XML is generated. And then complete the entire XML,

echo $dom->savexml ();

SaveXML () can also enter only part of the XML text, such as Echo $dom->savexml ($link), and only output <link> nodes: <link>http://blog.csdn.net/ Morewindows/article/details/7102362</link>

Here is a complete example of the output of data in PHP to an XML file. This example outputs 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 = arr Ay (
"first" => Array (
"title" => "PHP access to MySQL database", "
Link" => "http://blog.csdn.net/morewindows/ article/details/7102362 "
),
" second "=> Array (
" title "=>" PHP access to MySQL database Intermediate article smarty Technology ",
" link "=>" h ttp://blog.csdn.net/morewindows/article/details/7094642 "
),
" third "=> Array (
" title "=>") PHP access to MySQL database advanced Ajax Technology ",
Link" => "http://blog.csdn.net/morewindows/article/details/7086524"
),
); br> $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 ';
?>
<?php
//Output the array to an XML file
//by Morewindows (http://blog.csdn.net/MoreWindows)
$article _array = Array (
First => Array (
"title" => "PHP access to MySQL database primary article",
Link "=>" http://blog.csdn.net/morewindows /article/details/7102362 "
),
" second "=> Array (
" title "=>" PHP access to MySQL database Intermediate article smarty Technology ",
" link "=> http://blog.csdn.net/morewindows/article/details/7094642 "
),
" third "=> Array (
" title "=>") PHP access to 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). ' Byte ';

To run this PHP will generate Test.xml files on D disk (Win7 + XAMPP + IE9.0 test pass)

Two reading XML files
Take the d:test.xml generated in the previous reading 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 <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);
}

//outputting results
echo "<pre>";
Var_dump ($article _array);
echo "</pre>";
?>
<?php
//Read XML file
//by Morewindows (http://blog.csdn.net/MoreWindows)
$filename = "D:test.x ML ";
$article _array = arRay ();

$dom = new DOMDocument (' 1.0 ', ' UTF-8 ');
$dom->load ($filename);

Get the <article> knots.
$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>";
?>

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.