Four ways to generate XML files using PHP
Title1
Content1
2009-10-11
Title2
Content2
2009-11-11
"Generate string Directly"
Method 1: Generate the string using pure PHP code and write the string to a file that is suffixed with XML. This is the most primitive method of generating 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 an XML 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: Generate an XML file using DOMDocument
Create nodes using the CreateElement method,
Create text content using the createTextNode method,
Add child nodes using the AppendChild method,
To create an attribute 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 ',
)
);
Property 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 ');
Creating the 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) {
Creating elements
$ $key = $dom->createelement ($key);
$item->appendchild ($ $key);
Creating element values
$text = $dom->createtextnode ($val);
$ $key->appendchild ($text);
if (Isset ($attribute [$key])) {
If there are related attributes in this field, you need to set
foreach ($attribute [$key] as $akey = = $row) {
To create an attribute node
$ $akey = $dom->createattribute ($akey);
$ $key->appendchild ($ $akey);
Creating attribute Value Nodes
$aval = $dom->createtextnode ($row);
$ $akey->appendchild ($aval);
}
}//End If
}
}//End If
}//End Function
?>
"XMLWriter"
Method 3: Create an XML file using the XmlWriter class
This method is valid after PHP 5.1.2
In addition, it can output a variety of encoded 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 ',
)
);
Property array
$attribute _array = Array (
' title ' = = Array (
' Size ' = 1
)
);
$xml = new XMLWriter ();
$xml->openuri ("Php://output");
Output mode, can also be set to an XML file address, output directly into a file
$xml->setindentstring (");
$xml->setindent (TRUE);
$xml->startdocument (' 1.0 ', ' utf-8 ');
Start creating a 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) {
Setting property values
$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: Create an XML document using SimpleXML
$data _array = Array (
Array
' Title ' = ' Title1 ',
' Content ' = ' content1 ',
' pubdate ' = ' 2009-10-11 ',
),
Array
' Title ' = ' Title2 ',
' Content ' = ' content2 ',
' pubdate ' = ' 2009-11-11 ',
)
);
Property 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]))
{
foreach ($attribute _array[$key] as $akey = = $aval) {
Setting property values
$node->addattribute ($akey, $aval);
}
}
}
}
}
echo $xml->asxml ();
?>
http://www.bkjia.com/PHPjc/363956.html www.bkjia.com true http://www.bkjia.com/PHPjc/363956.html techarticle four 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 ...