PHP generates XML and transforms [source code]

Source: Internet
Author: User
Keywords PHP generates XML and transforms [source code]

PHP generates XML just like a tree, adding nodes one by one, and you can add multiple child nodes under a parent node:

function Madexml () {//get template information $strTempInfo = $this->modelcmsobj->gettemplate (' 2007 ');      $arrTemp = Explode ("#", $strTempInfo);      Array_shift ($arrTemp);      $arrContents = Array ();      foreach ($arrTemp as $k = + $v) {$arrContents [$k]=explode (",", $v);      }//Parse to XML file $objDom = new DOMDocument ("1.0");       Header ("Content-type:text/plain");      add element and Text nodes $root = $objDom->createelement ("recommend");      $objDom->appendchild ($root);  foreach ($arrContents as $k = = $v) {$item = $objDom->createelement ("entry");  $root->appendchild ($item);  $nextitem 1 = $objDom->createelement ("Simgurl");  $nextitem 2 = $objDom->createelement ("Imgurl");  $nextitem 3 = $objDom->createelement ("FileURL");  $item->appendchild ($nextitem 1);  $item->appendchild ($nextitem 2);  $item->appendchild ($nextitem 3);  $text 1 = $objDom->createtextnode ($v [0]);  $text = $objDom->createtextnode ($v [1]); $text 2 = $objDom->createtExtnode ($v [2]);  $nextitem 2->appendchild ($text 1);  $nextitem 3->appendchild ($text 2);  $nextitem 1->appendchild ($text);  } echo $objDom->savexml (); }

Simplexml_load_string converting XML strings to Strings

$string = <<
 
  
     
  
    
   
   Forty what?  
   
   
    
    Joe
   
     
   
   
    
    Jane
   
         I know that ' s the answer--and what's the ' s the question?< c15/>
    XML;  $xml = simplexml_load_string ($string);  Var_dump ($xml); 
 

The above code will output:

SimpleXMLElement Object  (    [title] = Forty What?    [from] + Joe    [to] + Jane    [body] =     I know that's the answer--but what ' s the question?  )  

If you want to convert the XML to an array, convert it to a string that contains a key value, and then loop it to be a list:

/**   * XML conversion to an array   * @param unknown_type $xml   */Private Function Xml_to_array ($xml)  {  $array = (array) (Simplexml_load_string ($xml, ' simplexmlelement ', libxml_nocdata));  foreach ($array as $key = + $item) {  $array [$key]  = $this->struct_to_array ((array) $item);  }  return $array;}  Private Function Struct_to_array ($item) {  if (!is_string ($item)) {  $item = (array) $item;  foreach ($item as $key = + $val) {  $item [$key]  =  Self::struct_to_array ($val);}  }  return $item;  
  • 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.