Questions about generating xml documents in php

Source: Internet
Author: User
Php generated xml document problem php generated xml, I found four methods on the Internet, but when I did the result, the process is as follows: the xml to be output is: & lt ;? Xml & nbsp; version = "1.0" & nbsp; encoding = "UTF-8 "? & Gt; & nbsp; & lt; xml document generated by artic php
Php generates xml. I searched the internet for four methods, but when I did it myself, the result was incorrect. The process is as follows:
The xml to be output is:



Title1
Content1
2009-10-


Title2
Content2


 

For example, if I use SimpleXML to create an XML document, the code is as follows (all are online examples ):
 $ 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 );
}
}
}

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.