$xml .= create_item($data['title'], $title_size, $data['content'], $data[' pubdate']);
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
$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();
?> ;
http://www.bkjia.com/PHPjc/326075.htmlwww.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...