Four methods for generating XML files in PHP _ PHP Tutorial

Source: Internet
Author: User
PHP generates XML files in four ways to share. The following XML code is generated to copy the code :? Xmlversion1.0encodingutf-8? The articleitemtitlesize1title1titlecontentcontent1contentpubdate2009-10-11pubdat generates the following XML string
Xml code

The code is as follows:





Title1
Content1
2009-10-11


Title2
Content2




Method I. [directly generate a string]
Use pure PHP code to generate a string and write it into a file suffixed with XML. This is the most primitive method for generating XML, but it is valid!

The code is as follows:


$ Data_array = array (
Array (
'Title' => 'title1 ',
'Content' => 'content1 ',
'Pubdate' => '2017-10-11 ',
),
Array (
'Title' => 'title2 ',
'Content' => 'content2 ',
'Pubdate' => '2017-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 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;
}
?>


Method 2: [DomDocument]
Use DomDocument to generate XML files, create nodes using the createElement method, create text content using the createTextNode method, add subnodes using the appendChild method, and create attributes using the createAttribute method

The code is as follows:


$ Data_array = array (
Array (
'Title' => 'title1 ',
'Content' => 'content1 ',
'Pubdate' => '2017-10-11 ',
),
Array (
'Title' => 'title2 ',
'Content' => 'content2 ',
'Pubdate' => '2017-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 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 ){
// Create element
$ Key = $ dom-> createElement ($ key );
$ Item-> appendchild ($ key );
// Create element values
$ Text = $ dom-> createTextNode ($ val );
$ Key-> appendchild ($ text );
If (isset ($ attribute [$ key]) {
// If this field has related attributes, you need to set them.
Foreach ($ attribute [$ key] as $ akey => $ row ){
// Create an attribute node
$ Akey = $ dom-> createAttribute ($ akey );
$ Key-> appendchild ($ akey );
// Create a property value node
$ Aval = $ dom-> createTextNode ($ row );
$ Akey-> appendChild ($ aval );
}
} // End if
}
} // End if
} // End function
?>


Method 3: [XMLWriter]
Use the XMLWriter class to create an XML file. this method is valid after PHP 5.1.2. In addition, it can output multi-encoding XML, but the input can only be UTF-8

The code is as follows:


$ Data_array = array (
Array (
'Title' => 'title1 ',
'Content' => 'content1 ',
'Pubdate' => '2017-10-11 ',
),
Array (
'Title' => 'title2 ',
'Content' => 'content2 ',
'Pubdate' => '2017-11-11 ',
)
);
// Attribute array
$ Attribute_array = array (
'Title' => array (
'Size' => 1
)
);
$ Xml = new XMLWriter ();
$ Xml-> openUri ("php: // output ");
// Output mode, which can also be set to an xml file address and directly output to a file
$ Xml-> setIndentString ('');
$ Xml-> setIndent (true );
$ Xml-> startDocument ('1. 0', 'utf-8 ');
// Start file creation
// 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 the attribute value
$ Xml-> writeAttribute ($ akey, $ aval );
}
}
$ Xml-> text ($ row); // sets the content
$ Xml-> endElement (); // $ key
}
}
$ Xml-> endElement (); // item
}
$ Xml-> endElement (); // article
$ Xml-> endDocument ();
$ Xml-> flush ();
?>


Method 4: [SimpleXML]
Use SimpleXML to create an XML document

The code is as follows:


$ Data_array = array (
Array (
'Title' => 'title1 ',
'Content' => 'content1 ',
'Pubdate' => '2017-10-11 ',
),
Array (
'Title' => 'title2 ',
'Content' => 'content2 ',
'Pubdate' => '2017-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])
{
Foreach ($ attribute_array [$ key] as $ akey => $ aval ){
// Set the attribute value
$ Node-> addAttribute ($ akey, $ aval );
}
}
}
}
}
Echo $ xml-> asXML ();
?>

The pipeline Xml code is as follows :? Xml version = "1.0" encoding = "UTF-8 "? Article item title size = "1" title1/title contentcontent1/content pubdate2009-10-11/pubdat...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.