Four ways to generate XML files using PHP
[Generate string directly]
Method 1: Use pure PHP code to generate a string and write the string to a file with XML as suffix . This is the most primitive way to generate XML, but it works!
The PHP 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',
)
);
$title_size = 1;
$ xml = "n";
$xml .= "
foreach ($data_array as $data) {
$xml .= create_item($data['title'], $title_size, $data['content'], $data['pubdate']);
}
$xml .= "
echo $xml;
// Create XML single item
function create_item($title_data, $title_size, $content_data , $pubdate_data)
{
$item = "
$item .= "
$item .= "
$item .= "
$item .= "
return $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 the createAttribute method
The PHP 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 a XML document and set 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 ) {
// Create element
$$key = $dom->createElement($key);
$item->appendchild($$key);
// Create element value
$text = $dom->createTextNode($val);
$$key->appendchild($text);
if (isset($attribute[$key ])) {
// If this field has related attributes, you need to set
foreach ($attribute[$key] as $akey => $row) {
// Create attribute node
$ $akey = $dom->createAttribute($akey);
$$key->appendchild($$akey);
// Create attribute value node
$aval = $dom ->createTextNode($row);
$$akey->appendChild($aval);
}
} // end if
}
} // end if
} // end function
?>
[XMLWriter]
Method 3: Use the XMLWriter class to create an XML file
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
The PHP 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) {
$xml->startElement($key);
if (isset($attribute_array[$key]) && is_array($attribute_array[$key]))
{
foreach ($attribute_array[$key] as $akey => $aval) {
//Set attribute value
$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: Use SimpleXML to create XML document
$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 = <<
< ;/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) {
//Set attribute value
$node->addAttribute($akey, $aval);
}
}
}
}
}
echo $xml->asXML();
?>