Php generate xml and convert [source code] _ PHP Tutorial

Source: Internet
Author: User
Php generates xml and converts [source code]. Php generates xml like a tree. you can add nodes one by one. you can add multiple subnodes under a parent node, functionmadexml () {get template information $ strTempInfo $ this-modelCmsObj-g php generates xml like a tree. add 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 (2007 );
  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. Echo $ objDom-> saveXML ();
  35. }
Simplexml_load_string converts an xml string to a string
 
 
  1. $ String = <
  2. Forty What?
  3. Joe
  4. Jane
  5. I know thats the answer -- but whats the question?
  6. XML;
  7. $ Xml = simplexml_load_string ($ string );
  8. Var_dump ($ xml );
  9. ?>
  10. This script will display:

    Templates, function madexml () {// Obtain template information $ strTempInfo = $ this-modelCmsObj-g...

Related Article

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.