搜尋
首頁php教程php手册讲解PHP生成xml源代码

讲解PHP生成xml源代码

Jun 13, 2016 am 10:40 AM
phpxml一樣添加原始碼父親產生節點講解

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串转化为字符串
    $string =        
      
    Forty What?  
    Joe  
    Jane  
      
    I know thats the answer -- but whats the question?  
      
    
 
    XML;  
    $xml = simplexml_load_string($string);  
    var_dump($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;  
   }

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中