Home > Backend Development > PHP Tutorial > Four ways to generate XML files using PHP_PHP Tutorial

Four ways to generate XML files using PHP_PHP Tutorial

WBOY
Release: 2016-07-21 14:57:55
Original
988 people have browsed it

Four ways to generate XML files using PHP




title1< ;/title> <br><content>content1</content> <br><pubdate>2009-10-11</pubdate> <br></item> <br><item> <br> <title size="1">title2
content2
2009-11-11


[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 .= "
n";

foreach ($data_array as $data) {
$xml .= create_item($data['title'], $title_size, $data['content'], $data['pubdate']);
}

$xml .= "
n";

echo $xml;

// Create XML single item
function create_item($title_data, $title_size, $content_data , $pubdate_data)
{
$item = "n";
$item .= "" . $title_data . "n";
$item .= "" . $content_data . "n";
$item .= " " . $ pubdate_data . "n";
$item .= "
n";

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();
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/363956.htmlTechArticleFour ways to generate XML files using PHP?xml version=1.0 encoding=utf-8? article item title size =1title1/title contentcontent1/content pubdate2009-10-11/pubdate /item item title si...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template