thinkphp XML Encoding functions

Source: Internet
Author: User

/** * XML encoding * @param mixed $data data * @param string $root root node name * @param string $item Number index sub-section name * @param string $attr root Property * @param string $id Numeric index child node key conversion property name * @param string $encoding Data encoding * @return string*/functionXml_encode ($data,$root= ' Think ',$item= ' Item ',$attr= ",$id= ' id ',$encoding= ' Utf-8 ') {    if(Is_array($attr)){        $_attr=Array(); foreach($attr  as $key=$value) {            $_attr[] = "{$key}=\"{$value}\""; }        $attr=implode(‘ ‘,$_attr); }    $attr=Trim($attr); $attr=Empty($attr) ? ‘‘ : " {$attr}"; $xml= "<?xml version=\" 1.0\ "encoding=\" {$encoding}\ "?>"; $xml. = "<{$root}{$attr}> "; $xml. = Data_to_xml ($data,$item,$id); $xml. = "</{$root}> "; return $xml;}/** * Data XML encoding * @param mixed $DATA Data * Node name when string $item numeric index * @param string $id numeric index key converted to the property name * @return S Tring*/functionData_to_xml ($data,$item= ' Item ',$id= ' id ') {    $xml=$attr= ' '; foreach($data  as $key=$val) {        if(Is_numeric($key)){            $id&&$attr= " {$id}=\"{$key}\""; $key=$item; }        $xml. = "<{$key}{$attr}> "; $xml.=  (Is_array($val) ||Is_object($val)) ? Data_to_xml ($val,$item,$id) :$val; $xml. = "</{$key}> "; }    return $xml;}

thinkphp XML Encoding functions

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.