PHP Read and write XML file tips _php Tutorial

Source: Internet
Author: User
The following lines are commonly used:

Header ("content-type:text/html; Charset=utf-8 "); Specify PHP to use UTF-8 encoding
$xml = simplexml_load_file ("Example.xml"); Reading an XML file
$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. Below is a brief description of how these classes are used.

A Generating an XML file
For one of the following XML files.

[HTML]

PHP access MySQL Database primary article
http://blog.csdn.net/morewindows/article/details/7102362



PHP access MySQL Database primary article
http://blog.csdn.net/morewindows/article/details/7102362

Let's see how PHP can be used to generate:

Start with a new DOMDocument object and set the encoding format.

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

$dom->formatoutput= true;

Then create the node andJunction </p>

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

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

Then create a node with text content

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

You can also create nodes before adding text to them.

$link = $dom->createelement ("link");

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

$link->appendchild ($linktext);

Then theAnd Nodes are added to the node.

$rootelement->appendchild ($title);

$rootelement->appendchild ($link);

Finally, the nodes are 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 some XML text, such as Echo $dom->savexml ($link); only output nodes: http://blog.csdn.net/morewindows/article /details/7102362

Here is a complete example of the output of the data content in PHP to an XML file. This example prints a PHP array to an XML file.

[PHP] Outputting an array to an XML file
by Morewindows (http://blog.csdn.net/MoreWindows)
$article _array = Array (
"First article" = = Array (
"title" + "PHP access MySQL database primary article",
"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"
),
"Third article" = = 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 ';
?>
Outputting an array to an XML file
by Morewindows (http://blog.csdn.net/MoreWindows)
$article _array = Array (
"First article" = = Array (
"title" + "PHP access MySQL database primary article",
"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"
),
"Third article" = = 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 pass)

Two Reading an XML file
For example, read the d:test.xml generated in the previous article:

[PHP] Reading an 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 knot.
$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 results
echo "



echo "
";
?>
Reading an 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 knot.
$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 results
echo "

";
Var_dump ($article _array);
echo "
";
?>

http://www.bkjia.com/PHPjc/371866.html www.bkjia.com true http://www.bkjia.com/PHPjc/371866.html techarticle The following lines are commonly used: 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 (...

  • Related Article

    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.