The 4 common ways to generate XML files using PHP are as follows: "XML file Contents"<?xml version= "1.0" encoding= "Utf-8"?><article> <item> <title size= "1" >title1</title& Gt <content>content1</content> <pubdate>2009-10-11</pubdate> </item> <item> <title size= "1" >title2</title> <content>content2</content> <pubdate>200 9-11-11</pubdate> </item></article>"Generate string Directly" Method 1: Generate a string using pure PHP code and write the string to a file that is suffixed with XML. This is the most primitive method of generating XML, but it works! The PHP code is as follows:<?PHP$data _array=Array( Array( ' Title ' = ' title1 ', ' content ' = ' content1 ', ' pubdate ' and ' 2009-10-11 ', ),Array( ' Title ' = ' title2 ', ' content ' = ' content2 ', ' pubdate ' and ' 2009-11-11 ', ));$title _size= 1; $xml= "<?xml version=\" 1.0\ "encoding=\" utf-8\ "? >\n";$xml. = "<article>\n"; foreach($data _array as $data) { $xml. = Create_item ($data[' title '],$title _size,$data[' content '],$data[' pubdate ']);} $xml. = "</article>\n"; Echo $xml; //Create an XML itemfunctionCreate_item ($title _data,$title _size,$content _data,$pubdate _data) { $item= "<item>\n"; $item. = "<title size=\" ".$title _size. "\" > ".$title _data. "</title>\n"; $item. = "<content>".$content _data. "</content>\n"; $item. = "<pubdate>".$pubdate _data. "</pubdate>\n"; $item. = "</item>\n"; return $item;} ?>"DomDocument" Method 2: Use the DomDocument to generate the XML file to create the node using the CreateElement method, create the text content using the createTextNode method, add child nodes using the AppendChild method, To create a property using the CreateAttribute method PHP code is as follows:<?PHP$data _array=Array( Array( ' Title ' = ' title1 ', ' content ' = ' content1 ', ' pubdate ' and ' 2009-10-11 ', ),Array( ' Title ' = ' title2 ', ' content ' = ' content2 ', ' pubdate ' and ' 2009-11-11 ', )); //Property Array$attribute _array=Array( ' Title ' =Array( ' Size ' = 1 )); //create an XML document and set the XML version and encoding. $dom=NewDomDocument (' 1.0 ', ' utf-8 '); //Creating the root node$article=$dom->createelement (' article ');$dom->appendchild ($article); foreach($data _array as $data) { $item=$dom->createelement (' item ')); $article->appendchild ($item); Create_item ($dom,$item,$data,$attribute _array);} Echo $dom-savexml ();functionCreate_item ($dom,$item,$data,$attribute) { if(Is_array($data)) { foreach($data as $key=$val) { //Creating Elements$$key=$dom->createelement ($key); $item->appendchild ($$key); //Creating element Values $text=$dom->createtextnode ($val); $$key->appendchild ($text); if(isset($attribute[$key])) {//if there are related attributes in this field, you need to set foreach($attribute[$key] as $akey=$row) { //To Create an attribute node$$akey=$dom->createattribute ($akey); $$key->appendchild ($$akey); //Creating attribute value nodes $aval=$dom->createtextnode ($row); $$akey->appendchild ($aval); } } //End If } } //End If}//End Function?>"XMLWriter" Method 3: Create an XML file using the XMLWriter class this method in PHP5.1.valid after 2 In addition, it can output a variety of encoded XML, but the input can only be UTF-8The PHP code is as follows:<?PHP$data _array=Array( Array( ' Title ' = ' title1 ', ' content ' = ' content1 ', ' pubdate ' and ' 2009-10-11 ', ),Array( ' Title ' = ' title2 ', ' content ' = ' content2 ', ' pubdate ' and ' 2009-11-11 ', )); //Property Array$attribute _array=Array( ' Title ' =Array( ' Size ' = 1 )); $xml=NewXMLWriter ();$xml->openuri ("Php://output");//output mode, can also be set to an XML file address, output directly into a file$xml->setindentstring (' ');$xml->setindent (true); $xml->startdocument (' 1.0 ', ' utf-8 ');//Start creating files//Root nodes$xml->startelement (' article '); foreach($data _array as $data) { $xml->startelement (' item ')); if(Is_array($data)) { foreach($data as $key=$row) { $xml->startelement ($key); if(isset($attribute _array[$key]) &&Is_array($attribute _array[$key])) { foreach($attribute _array[$key] as $akey=$aval) {//Setting property values $xml->writeattribute ($akey,$aval); } } $xml->text ($row);//Set Content $xml->endelement ();//$key } } $xml->endelement ();//Item} $xml->endelement ();//article$xml-enddocument ();$xml-Flush();?>"SimpleXML" Method 4: Create an XML document using SimpleXML<?PHP$data _array=Array( Array( ' Title ' = ' title1 ', ' content ' = ' content1 ', ' pubdate ' and ' 2009-10-11 ', ),Array( ' Title ' = ' title2 ', ' content ' = ' content2 ', ' pubdate ' and ' 2009-11-11 ', )); //Property Array$attribute _array=Array( ' Title ' =Array( ' Size ' = 1 )); $string= <<<XML<?xml version= ' 1.0 ' encoding= ' utf-8 '?><article></article>XML;$xml=simplexml_load_string($string); foreach($data _array as $data) { $item=$xml->addchild (' item ')); if(Is_array($data)) { foreach($data as $key=$row) { $node=$item->addchild ($key,$row); if(isset($attribute _array[$key]) &&Is_array($attribute _array[$key])) { foreach($attribute _array[$key] as $akey=$aval) {//Setting property values $node->addattribute ($akey,$aval); } } } }}Echo $xml-asxml ();?>
PHP generates an XML file