PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > PHP教程 > 正文

    PHP之SimpleXML函数

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:使用php创建XML文件十分简单,使用SimpleXML那就更简便了,同时读取XML文件也十分方便。XML文件是直接在浏览器中打开,以自定义标签的方式...
    使用php创建XML文件十分简单,使用SimpleXML那就更简便了,同时读取XML文件也十分方便。XML文件是直接在浏览器中打开,以自定义标签的方式直观简洁的方式展示给读者。

    1.创建XML文件

    1. header("Content-type: text/html; charset=utf-8");  
    2.     $xml=new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><returnRequest />'); 
    3.     $item=$xml->addchild("client","DYSON"); 
    4.     $item1=$xml->addchild("distributionCentre","DAMCO"); 
    5.      
    6.     $item2=$xml->addchild("order"); 
    7.     $item2->addchild("ref",$info_all['id']); 
    8.     $item2->addchild("id","??"); 
    9.     $item2->addchild("store","CN"); 
    10.         $item3 = $item2->addchild("detail"); 
    11.             $item3->addchild("created",$info_all['crated']); 
    12.             $item3->addchild("customer"); 
    13.             $item3->addchild("ip"); 
    14.             $item3->addchild("language","cn-GB"); 
    15.             $item3->addchild("vatCountry","CN"); 
    16.             $item3->addchild("origin","DYSON"); 
    17.             $item3->addchild("originDate",$info_all['crated']); 
    18.             $item3->addchild("customerReference","???"); 
    19.             $item3->addchild("csAgent"); 
    20.         $item4 = $item2->addchild("people"); 
    21.             $item4_1 = $item4->addchild("person"); 
    22.             $item4_1->addchild("ref"); 
    23.             $item4_1->addchild("title"); 
    24.             $item4_1->addchild("firstName",$info_all['receiver_name']); 
    25.             $item4_1->addchild("lastName"); 
    26.             $item4_1->addchild("phone",$info_all['receiver_mobile']); 
    27.             $item4_1->addchild("fax"); 
    28.             $item4_1->addchild("mobile"); 
    29.             $item4_1->addchild("email"); 
    30.             $item4_1->addchild("department"); 
    31.             $item4_1->addchild("companyName"); 
    32.             $item4_1->addchild("gender"); 
    33.             $item4_1->addchild("dateofbirth"); 
    34.         $item5 = $item2->addchild("address"); 
    35.             $item5_1 = $item5->addchild("address"); 
    36.             $item5_1->addchild("addresstype","customer"); 
    37.             $item5_1->addchild("addrss1",$info_all['receiver_district']); 
    38.             $item5_1->addchild("addrss2",$info_all['receiver_address']); 
    39.             $item5_1->addchild("city",$info_all['receiver_city']); 
    40.             $item5_1->addchild("state",$info_all['receiver_state']); 
    41.             $item5_1->addchild("zip",$info_all['receiver_zip']); 
    42.                 $item5_1_1 = $item5_1->addchild("country"); 
    43.                 $item5_1_1->addchild("code","CN"); 
    44.                 $item5_1_1->addchild("name","CHINA"); 
    45.              
    46.          
    47.          
    48.          
    49.     header("Content-type: text/xml"); 
    50.     // echo $xml->asXml();exit; 
    51.     $xml->asXml("test.xml"); 

    使用addchild方法可以无限创建XML标签,同时也可以无限层级,类似多维数组形式。文件打开显示为

    PHP之SimpleXML函数

    2.解析XML文件

    1. $xml = simplexml_load_file("test.xml"); 
    2.     
    3.      $data['client'] = $xml->client; 
    4.      $data['language'] = $xml->order->detail->language; 
    5.  
    6.      echo $data['language']; 

    使用 simplexml_load_file 函数可以解析XML文件 可以获取指定标签中的数据 (->标签)箭头指向哪个标签便获取所在标签中的数据。
    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-10-5793-1.html
    相关热词搜索: