Generate the following XML string
XML code
Copy CodeThe code is as follows:
Title1
Content1
2009-10-11
Title2
Content2
2009-11-11
method I. "Generate string directly"
Generates a string using pure PHP code and writes the string to a file that is suffixed with XML. This is the most primitive method of generating XML, but it works!
Copy CodeThe 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;
}
?>
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, To create an attribute using the CreateAttribute method
Copy CodeThe 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
?>
Method 3: "XMLWriter"
Create an XML file using the XmlWriter class, which 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
Copy CodeThe 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 ();
?>
Method 4: "SimpleXML"
Creating an XML document using SimpleXML
Copy CodeThe 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
)
);
$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/326075.html www.bkjia.com true http://www.bkjia.com/PHPjc/326075.html techarticle generate the following XML string XML code to copy the code code as follows:? xml version= "1.0" encoding= "utf-8"? Article item title size= "1" title1/title contentcontent 1/content Pubdate2009-10-11/pubdat ...