4 methods for generating XML files in PHP

Source: Internet
Author: User

Generate the following XML string
Xml Code
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Article>
<Item>
<Title size = "1"> title1 </title>
<Content> content1 </content>
<Pubdate> 2009-10-11 </pubdate>
</Item>
<Item>
<Title size = "1"> title2 </title>
<Content> content2 </content>
<Pubdate> 2009-11-11 </pubdate>
</Item>
</Article>

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!
Copy codeThe Code is as follows:
<? PHP
$ 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 = "<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> \ N ";
$ Xml. = "<article> \ n ";
Foreach ($ data_array as $ data ){
$ Xml. = create_item ($ data ['title'], $ title_size, $ data ['content'], $ data ['pubdate']);
}
$ Xml. = "</article> \ n ";
Echo $ xml;
// Create an XML single item
Function create_item ($ title_data, $ title_size, $ content_data, $ pubdate_data)
{
$ Item = "<item> \ n ";
$ Item. = "<title size = \" ". $ title_size." \ ">". $ title_data. "</title> \ n ";
$ Item. = "<content>". $ content_data. "</content> \ n ";
$ Item. = "<pubdate>". $ pubdate_data. "</pubdate> \ n ";
$ Item. = "</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
Copy codeThe Code is as follows:
<? Php
$ 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
Copy codeThe Code is as follows:
<? Php
$ 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
Copy codeThe Code is as follows:
<? Php
$ 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 version = '1. 0' encoding = 'utf-8'?>
<Article>
</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 the attribute value
$ Node-> addAttribute ($ akey, $ aval );
}
}
}
}
}
Echo $ xml-> asXML ();
?>

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.