您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

讲解PHP生成xml源代码_PHP教程

2025/10/13 7:35:20发布20次查看
php生成xml就像树一样,逐个添加节点,可以在一个父节点下添加多个子节点,
function madexml()
{  
        //获取模板信息  
        $strtempinfo = $this->modelcmsobj->gettemplate(2007);  
        $arrtemp = explode(#,$strtempinfo);  
        array_shift($arrtemp);  
        $arrcontents = array();  
        foreach($arrtemp as $k=>$v)  
        {  
                $arrcontents[$k]=explode(,,$v);  
        }  
        //解析为xml文件  
        $objdom = new domdocument(1.0);  
        header(content-type: text/plain);   
        //添加元素和文本节点  
        $root = $objdom->createelement(recommend);  
        $objdom->appendchild($root);  
        foreach($arrcontents as $k=>$v)  
        {  
                $item = $objdom->createelement(entry);  
                $root->appendchild($item);  
                $nextitem1 = $objdom->createelement(simgurl);  
                $nextitem2 = $objdom->createelement(imgurl);  
                $nextitem3 = $objdom->createelement(fileurl);  
                $item->appendchild($nextitem1);  
                $item->appendchild($nextitem2);  
                $item->appendchild($nextitem3);  
                $text1 = $objdom->createtextnode($v[0]);  
                $text = $objdom->createtextnode($v[1]);  
                $text2 = $objdom->createtextnode($v[2]);  
                $nextitem2->appendchild($text1);  
                $nextitem3->appendchild($text2);  
                $nextitem1->appendchild($text);  
        }  
        echo $objdom->savexml();  
}
simplexml_load_string把xml串转化为字符串
this script will display:   
   simplexmlelement object  
     (  
     [title] => forty what?  
     [from] => joe  
     [to] => jane  
     [body] =>  
      i know thats the answer -- but whats the question?  
   )
如果想把xml转换为数组,先转换为字符串含有键值的字符串,然后再进行循环就可以成为数组了,
    /**  
     * xml转换为数组  
     * @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;  
   }
http://www.bkjia.com/phpjc/486096.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/486096.htmltecharticlephp生成xml就像树一样,逐个添加节点,可以在一个父节点下添加多个子节点, function madexml() { //获取模板信息 $strtempinfo = $this-modelcmsobj-get...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product