PHP Create XML

Source: Internet
Author: User

PHP Create XML

PHP creates XML and saves it, learning examples are as follows:

<?php try{   Create DOMDocument Object $dom = new DOMDocument ("1.0", "Utf-8");   Formatted output $doc-formatoutput = true;   Create the root node and add it to the XML object $root = $dom createelement ("News");   $dom-AppendChild ($root);    $item = null; $txt = null; $attr = NULL;      Use a loop to create 20 sub-nodes for ($i =0; $i <; $i + +) {//Create child nodes and add content $item = $dom, createelement ("item");       $root-AppendChild ($item); Create the text content of the child node $txt = $dom createTextNode ("<![ cdata[I was the first "." $i + 1). " "The text content of the child node Oh!"       ]]> ");       $item-AppendChild ($txt);       Create child node Properties $attr = $dom-CreateAttribute ("Id");       $item-AppendChild ($attr);       Adds a value to the attribute node $txt = $dom-createTextNode ($i);   $attr-AppendChild ($txt); }//Save the XML $dom, save ($_server["Document_root"]. "   /file/phpxml.xml ");   }catch (Exception $e) {exit (); }?> 


Extension: If it is displayed in XML format only, it does not need to be saved as an XML file

You can refer to the following example:

<?php   Header ("Content-type:text/xml;charset=utf-8");? ><?xml version= "1.0" encoding= "Utf-8"? ><news><?php for   ($i =0; $i < $i + +) {   ?>      <item id= "<?php Echo ($i)?>" ><! [cdata[<?php Echo ($i + 1)?>, I am splicing generated XML Oh! ]]></item>  <?php  }?></news>

As follows:









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.