Home  >  Article  >  Backend Development  >  Share 4 ways to generate XML files in PHP_PHP Tutorial

Share 4 ways to generate XML files in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:15:42653browse

Generate the following XML string
encoding="utf-8"?>

                                                                                                                     ;/content> ; 2009-10-11 title2< /title></div>                                                                                                                  <div class="codebody" id="code70205"> <br><br>Method I. [Generate string directly] <br><br>Use pure PHP code to generate a string and write the string into a file with XML as suffix. This is the most primitive way to generate XML, but it works! <br><br><br><br>Copy code<br><br> The code is as follows:<br><br><br><?PHP<BR>$data_array = array(</div> array(<BR> 'title' => 'title1',<strong> 'content' => 'content1',<br> 'pubdate' => '2009-10-11',</strong> ),<br> array(<div class="codetitle"> 'title' => 'title2',<span style="CURSOR: pointer" onclick="doCopy('code61919')"> 'content' => 'content2',<u> 'pubdate' => '2009-11-11',</u> )</span>);</div>$title_size = 1;<div class="codebody" id="code61919">$xml = "<?xml version="1.0" encoding="utf-8"?>n";<br>$xml .= "< ;article>n";<br>foreach ($data_array as $data) {<br>$xml .= create_item($data['title'], $title_size, $data['content'], $data[' pubdate']);<br>}<br>$xml .= "</article>n";<br>echo $xml;<br>// Create XML single item<br>function create_item($title_data, $ title_size, $content_data, $pubdate_data)<br>{<br> $item = "<item>n";<br> $item .= "<title size="" . $title_size . "">" . $title_data . "n";
$item .= "" . $content_data . "n";
$item .= " n";
$item .= "n";
return $item;
}
?>



Method 2: [DomDocument]

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, and create attributes Use the createAttribute method



to copy the code

The code is as follows:

$data_array = array(
array(
'title' => 'title1',
'content' => 'content1',
       'pubdate' => '2009-10-11',
  ),
   array(
   'title' => 'title2',
   'content' => 'content2 ',
'pubdate' => '2009-11-11',
)
);
// Attribute array
$attribute_array = array(
'title' = > array(
'size' => 1
)
);
// Create an XML document and set the XML version and encoding. .
$dom=new DomDocument('1.0', 'utf-8');
// Create 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();
function create_item($dom, $item , $data, $attribute) {
if (is_array($data)) {
foreach ($data as $key => $val) {
$$key = $dom->createElement ($key); ; ($attribute[$key] as $akey => $row) {
                                                                                     $$key- >appendchild($$akey);
                                                                                       gt;appendChild($aval);
                                                                                                                                                             🎜> Method 3: [XMLWriter]

Use the 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 be utf-8



Copy code

The code is as follows:

$data_array = array(
array(
'title' => 'title1',
'content' => 'content1',
       'pubdate' => '2009-10-11',
  ),
   array(
   'title' => 'title2',
   'content' => 'content2 ',
'pubdate' => '2009-11-11',
)
);
// Attribute array
$attribute_array = array(
'title' = > array(
'size' => 1
)
);
$xml = new XMLWriter();
$xml->openUri("php://output ");
// The output method can also be set to an xml file address and directly output into a file
$xml->setIndentString(' ');
$xml->setIndent(true );
$xml->startDocument('1.0', 'utf-8');
// Start creating file
// Root node
$xml->startElement(' article');
foreach ($data_array as $data) {
$xml->startElement('item');
if (is_array($data)) {
foreach ($data as $key => $row) {
                                                                                                  using   with  --                                                            ​​​($akey, $aval);
                                                                                                                                  }
}
$xml->endElement(); // item
}
$xml->endElement(); // article
$xml->endDocument();
$xml ->flush();
?>



Method 4: [SimpleXML]

Create XML document using SimpleXML



Copy code

The code is as follows:
$data_array = array(
array( 'title' => 'title1', 'content' => 'content1', 'pubdate' => '2009-10-11', ),
array(
'title' => 'title2',
'content' => 'content2',
'pubdate' => '2009-11-11',
)
);
/ / Attribute array
$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]))
                                                                                           $node ->addAttribute($akey, $aval);
}
}
}
}
}
echo $xml->asXML();
?> ;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326075.htmlTechArticleGenerate the following XML string Xml code Copy the code as follows: ?xml version="1.0" encoding="utf-8 "? article item title size="1"title1/title contentcontent1/content pubdate2009-10-11/pubdat...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn