Php array converted to xml code

Source: Internet
Author: User
Php array converted to xml code

  1. Class ArrayToXML
  2. {
  3. /**
  4. * The main function for converting to an XML document.
  5. * Pass in a multi dimen1_array and this recrusively loops through and builds up an XML document.
  6. *
  7. * @ Param array $ data
  8. * @ Param string $ rootNodeName-what you want the root node to be-defaultsto data.
  9. * @ Param SimpleXMLElement $ xml-shoshould only be used recursively
  10. * @ Return string XML
  11. */
  12. Public static function toXml ($ data, $ rootNodeName = 'data', $ xml = null)
  13. {
  14. // Turn off compatibility mode as simple xml throws a wobbly if you don't.
  15. If (ini_get ('zend. zemo-compatibility_mode ') = 1)
  16. {
  17. Ini_set ('zend. zemo-compatibility_mode ', 0 );
  18. }
  19. If ($ xml = null)
  20. {
  21. $ Xml = simplexml_load_string (" <$ RootNodeName/> ");
  22. }
  23. // Loop through the data passed in.
  24. Foreach ($ data as $ key => $ value)
  25. {
  26. // No numeric keys in our xml please!
  27. If (is_numeric ($ key ))
  28. {
  29. // Make string key...
  30. $ Key = "unknownNode _". (string) $ key;
  31. }
  32. // Replace anything not alpha numeric
  33. $ Key = preg_replace ('/[^ a-z]/I', '', $ key );
  34. // If there is another array found recrusively call this function
  35. If (is_array ($ value ))
  36. {
  37. $ Node = $ xml-> addChild ($ key );
  38. // Recrusive call.
  39. ArrayToXML: toXml ($ value, $ rootNodeName, $ node );
  40. }
  41. Else
  42. {
  43. // Add single node.
  44. $ Value = htmlentities ($ value );
  45. $ Xml-> addChild ($ key, $ value );
  46. }
  47. }
  48. // Pass back as string. or simple xml object if you want!
  49. Return $ xml-> asXML ();
  50. }
  51. }

2. code for converting php array to xml written by myself

  1. Function arrtoxml ($ arr, $ dom = 0, $ item = 0 ){
  2. If (! $ Dom ){
  3. $ Dom = new DOMDocument ("1.0 ");
  4. }
  5. If (! $ Item ){
  6. $ Item = $ dom-> createElement ("root ");
  7. $ Dom-> appendChild ($ item );
  8. }
  9. Foreach ($ arr as $ key => $ val ){
  10. $ Itemx = $ dom-> createElement (is_string ($ key )? $ Key: "item ");
  11. $ Item-> appendChild ($ itemx );
  12. If (! Is_array ($ val )){
  13. $ Text = $ dom-> createTextNode ($ val );
  14. $ Itemx-> appendChild ($ text );
  15. } Else {
  16. Arrtoxml ($ val, $ dom, $ itemx );
  17. }
  18. }
  19. Return $ dom-> saveXML ();
  20. }
  21. ?>

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.