Php generates xml and converts [source code]

Source: Internet
Author: User
Php generates xml like a tree. you can add nodes one by one. you can add multiple subnodes under a parent node, functionmadexml () {// Obtain template information $ strTempInfo = $ this-modelCmsObj-getTemplate ('20140901'); $ arrTemp = explode (#, $ strTempI

Php generates xml like a tree, adding nodes one by one. you can add multiple subnodes under a parent node, function madexml ()

 

 
 
  1. {
  2. // Obtain template information
  3. $ StrTempInfo = $ this-> modelCmsObj-> getTemplate ('20140901 ');
  4. $ ArrTemp = explode ("#", $ strTempInfo );
  5. Array_shift ($ arrTemp );
  6. $ ArrContents = array ();
  7. Foreach ($ arrTemp as $ k => $ v)
  8. {
  9. $ ArrContents [$ k] = explode (",", $ v );
  10. }
  11. // Parse it into an xml file
  12. $ ObjDom = new DOMDocument ("1.0 ");
  13. Header ("Content-Type: text/plain ");
  14. // Add element and text node
  15. $ Root = $ objDom-> createElement ("recommend ");
  16. $ ObjDom-> appendChild ($ root );
  17. Foreach ($ arrContents as $ k => $ v)
  18. {
  19. $ Item = $ objDom-> createElement ("entry ");
  20. $ Root-> appendChild ($ item );
  21. $ Nextitem1 = $ objDom-> createElement ("simgurl ");
  22. $ Nextitem2 = $ objDom-> createElement ("imgurl ");
  23. $ Nextitem3 = $ objDom-> createElement ("fileurl ");
  24. $ Item-> appendChild ($ nextitem1 );
  25. $ Item-> appendChild ($ nextitem2 );
  26. $ Item-> appendChild ($ nextitem3 );
  27. $ Text1 = $ objDom-> createTextNode ($ v [0]);
  28. $ Text = $ objDom-> createTextNode ($ v [1]);
  29. $ Text2 = $ objDom-> createTextNode ($ v [2]);
  30. $ Nextitem2-> appendChild ($ text1 );
  31. $ Nextitem3-> appendChild ($ text2 );
  32. $ Nextitem1-> appendChild ($ text );
  33. }
  34.  
  35. Echo $ objDom-> saveXML ();
  36. }
Simplexml_load_string converts an xml string to a string
 
 
  1. $ String = <
  2. Forty What?
  3. Joe
  4. Jane
  5. I know that's the answer -- but what's the question?
  6. XML;
  7.  
  8. $ Xml = simplexml_load_string ($ string );
  9.  
  10. Var_dump ($ xml );
  11. ?>
  12.  
  13.  
  14. This script will display:
  15.  
  16. SimpleXMLElement Object
  17. (
  18. [Title] => Forty What?
  19. [From] => Joe
  20. [To] => Jane
  21. [Body] =>
  22. I know that's the answer -- but what's the question?
  23. )

If you want to convert xml to an array, first convert it to a string containing key values, and then loop it into an array,

 
 
  1. /**
  2. * Convert xml to an array
  3. * @ Param unknown_type $ xml
  4. */
  5. Private function xml_to_array ($ xml)
  6. {
  7. $ Array = (array) (simplexml_load_string ($ xml, 'simplexmlelement', LIBXML_NOCDATA ));
  8. Foreach ($ array as $ key => $ item ){
  9. $ Array [$ key] = $ this-> struct_to_array (array) $ item );
  10. }
  11. Return $ array;
  12. }
  13.  
  14. Private function struct_to_array ($ item ){
  15. If (! Is_string ($ item )){
  16. $ Item = (array) $ item;
  17. Foreach ($ item as $ key => $ val ){
  18. $ Item [$ key] = self: struct_to_array ($ val );
  19. }
  20. }
  21. Return $ item;
  22. }

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.