May I ask questions about converting arrays to XML formats?

Source: Internet
Author: User
Ask the great God array to XML format. This post was last edited by HUENKE from 2013-11-0417: 51: 55. the content stored in my PHP is a tree-based recursive array key = & gt; value is converted to XML format, but I need XML output in the following format. I debugged it for one afternoon. please help me & lt ;? For xml format conversion from arrays to XML
This post was last edited by HUENKE at 17:51:55, January 4 ,.

The content stored in PHP is a tree-like recursive array key => value
Convert to XML format
However
I need XML output in the following format

I debugged it for one afternoon. please help me




Type = "0">












Function arrayToXml ($ arr, $ xmlDoc = 0, $ item = 0)
{
/* Determine whether xml is used */
If (! $ XmlDoc)
{
$ XmlDoc = new DOMDocument ("1.0 ");
}
/* Determine whether the node is a subnode */
If (! $ Item)
{
$ Item = $ xmlDoc-> createElement ("nodes", "UTF-8 ");
$ XmlDoc-> appendChild ($ item );
}
/* Press "key => value" for array data to retrieve and write the data to the dom tree cyclically */
Foreach ($ arr as $ key => $ val)
{
/* Add the subnode name and value */
$ ItemX = $ xmlDoc-> createElement (is_string ($ key )? $ Key: "node ");
$ Item-> appendChild ($ itemX );
/* Perform recursion if the value of an element in the array is also an array */
If (! Is_array ($ val ))
{
$ Text = $ xmlDoc-> createTextNode ($ val );
$ ItemX-> appendChild ($ text );
}
Else
{
$ This-> arrayToXml ($ val, $ xmlDoc, $ itemX); // add $ this->
}
}
Return $ xmlDoc-> saveXML (); // return xml data. you can directly write the returned data to the *. xml file to generate an xml file.
}
Share To: value is converted to XML format, but I need XML output in the following format. I have debugged it for one afternoon. please help me
------ Solution --------------------
$ Ar = array (
Array ('id' => "c5569e2c-0f4a-4ec2-86ba-736b79eca18c", 'name' => "Company A", 'type' => "0 ",
Array ('id' => "4f2b55e9-e10a-496b-8bca-60e6f26daee5", 'name' => "department 1", 'type' => "1 ",
Array ('id' => "363", 'name' => "leaf 1", 'type' => "2", 'playurl' => "http: // XXXXXXX363 "),
),
Array ('id' => "0c5ce42f-ba31-4b7a-8173-79ecae4a73ca", 'name' => "Department 2", 'type' => "1 ",
Array ('id' => "241", 'name' => "leaf 1", 'type' => "2", 'playurl' => "http: // xxxxxxx = 241 "),
Array ('id' => "356", 'name' => "leaf 1", 'type' => "2 ", 'playurl' => "xxxxxxxxxxx = 356 "),
),
)
);
$ Xml = simplexml_load_string (' ');
ArrayToXml ($ ar, $ xml );
Echo $ xml-> asXML ();

Function arrayToXml ($ ar, $ xml ){
Foreach ($ ar as $ k => $ v ){
If (is_array ($ v) arrayToXml ($ v, $ xml-> addChild ('node '));
Else $ xml-> addAttribute ($ k, $ v );
}
}


------ Solution --------------------
Strategic mark

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.