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: