4 ways to generate XML files in PHP
title1 content1 2009-10-11 title2 content2 2009-11-11
【Generate string directly】
Method 1: Use pure PHP code to generate a string and write this string to a file with XML as the suffix. This is the most primitive method of generating XML, but it works!
The PHP code is as follows:
'title1', 'content' => 'content1', 'pubdate' => '2009-10-11', ), array( 'title' => 'title2', 'content' => 'content2', 'pubdate' => '2009-11-11', ) ); $title_size = 1; $xml = "\n"; $xml .= "\n"; foreach ($data_array as $data) { $xml .= create_item($data['title'], $title_size, $data['content'], $data['pubdate']); } $xml .= " \n"; echo $xml; // 创建XML单项 function create_item($title_data, $title_size, $content_data, $pubdate_data) { $item = "- \n"; $item .= "
\n"; return $item; } ?>" . $title_data . " \n"; $item .= "" . $content_data . " \n"; $item .= "" . $pubdate_data . " \n"; $item .= "
【DomDocument】
Method 2: Use DomDocument to generate XML files
Create nodes using the createElement method,
Create text content using the createTextNode method,
Add child nodes using the appendChild method,
Create attributes using createAttribute Method
PHP code is as follows:
'title1', 'content' => 'content1', 'pubdate' => '2009-10-11', ), array( 'title' => 'title2', 'content' => 'content2', 'pubdate' => '2009-11-11', ) ); // 属性数组 $attribute_array = array( 'title' => array( 'size' => 1 ) ); // 创建一个XML文档并设置XML版本和编码。。 $dom=new DomDocument('1.0', 'utf-8'); // 创建根节点 $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(); function create_item($dom, $item, $data, $attribute) { if (is_array($data)) { foreach ($data as $key => $val) { // 创建元素 $$key = $dom->createElement($key); $item->appendchild($$key); // 创建元素值 $text = $dom->createTextNode($val); $$key->appendchild($text); if (isset($attribute[$key])) { // 如果此字段存在相关属性需要设置 foreach ($attribute[$key] as $akey => $row) { // 创建属性节点 $$akey = $dom->createAttribute($akey); $$key->appendchild($$akey); // 创建属性值节点 $aval = $dom->createTextNode($row); $$akey->appendChild($aval); } } // end if } } // end if } // end function ?>
[XMLWriter]
Method 3: Use XMLWriter class to create XML files
This method is valid after PHP 5.1.2
In addition, it can output multiple encodings of XML, but the input can only It is utf-8
The PHP code is as follows:
'title1', 'content' => 'content1', 'pubdate' => '2009-10-11', ), array( 'title' => 'title2', 'content' => 'content2', 'pubdate' => '2009-11-11', ) ); // 属性数组 $attribute_array = array( 'title' => array( 'size' => 1 ) ); $xml = new XMLWriter(); $xml->openUri("php://output"); // 输出方式,也可以设置为某个xml文件地址,直接输出成文件 $xml->setIndentString(' '); $xml->setIndent(true); $xml->startDocument('1.0', 'utf-8'); // 开始创建文件 // 根结点 $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) { // 设置属性值 $xml->writeAttribute($akey, $aval); } } $xml->text($row); // 设置内容 $xml->endElement(); // $key } } $xml->endElement(); // item } $xml->endElement(); // article $xml->endDocument(); $xml->flush(); ?>
[SimpleXML]
Method 4: Use SimpleXML to create an XML document
'title1', 'content' => 'content1', 'pubdate' => '2009-10-11', ), array( 'title' => 'title2', 'content' => 'content2', 'pubdate' => '2009-11-11', ) ); // 属性数组 $attribute_array = array( 'title' => array( 'size' => 1 ) ); $string = <<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) { // 设置属性值 $node->addAttribute($akey, $aval); } } } } } echo $xml->asXML(); ?>
head command usage
what is nodejs
What to do if you can't delete files on your computer
Why is my phone not turned off but when someone calls me it prompts me to turn it off?
The difference between front-end and back-end
Introduction to ftp server usage
How to create a web page in python
How to check for plagiarism on CNKI Detailed steps for checking for plagiarism on CNKI