XMl入门介绍及php操作XML

来源:互联网 发布:同城团购用什么软件 编辑:程序博客网 时间:2024/05/17 21:43

一、什么是XML

XML全称:Extensible Markup Language
中文名:可扩展标记语言
用于标记电子文件使其具有结构性的标记语言,可以用来标记数据,定义数据类型,允许用户对自己的标记语言进行定义的源语言。
用于传输数据与存储数据

[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- 这是xml-->  
  3. <libray>  
  4.   <book id="1">  
  5.     <title><三国演义></title>  
  6.     <author>罗贯中</author>  
  7.     <price>82</price>  
  8.   </book>  
  9.   <book id="2">  
  10.     <title><水浒传></title>  
  11.     <author>施耐庵</author>  
  12.     <price>78</price>  
  13.   </book>  
  14. </libray>  


二、XML和html、json的对比

xml与html对比:

[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <html>  
  2.  <head>  
  3.   <title>html文件</title>  
  4.  </head>  
  5.  <body>  
  6.   <table>  
  7.   <tr>  
  8.     <th>姓名</th>  
  9.     <th>年龄</th>  
  10.   </tr>  
  11.   <tr>  
  12.     <td>张玲</td>  
  13.     <td>20</td>  
  14.   </tr>  
  15.   <tr>  
  16.     <td>李冰</td>  
  17.     <td>18</td>  
  18.   </tr>  
  19.   </table>  
  20.  </body>  
  21. </html>  


html不一定成对出现,XML则要求所有的标记必须成对出现;
  <br/>  <img />  <hr>
HTML标记不区分大小写,XML区分大小写。

xml与json对比:

[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. [{"id":1,"title":"\u4e09\u56fd\u6f14\u4e49","author":"\u7f57\u8d2f\u4e2d","price":80},{"id":2,"title":"\u6c34\u6d52\u4f20","author":"\u65bd\u8010\u5eb5","price":78}]  


它和json都是一种数据交换格式。
JSON(JavaScript Object Notation)一种轻量级的数据交换格式。数据格式比较简单,易于读写,格式都是压缩的,占用带宽小。易于解析,客户端JavaScript可以简单的通过eval()进行JSON数据的读取。

XML文件庞大,文件格式复杂,传输占带宽。
可以定义更复杂的数据结构,可读性比json数据好。
可读性强

三、XML应用场景

XML 是各种应用程序之间进行数据传输的最常用的工具。

应用场景:

1充当程序间交互的中间件(通讯标准)

快递100接口(JavaXMLPHP

2)配置文件

3)小型数据库

php+MySQL 金山词霸

php+xml 金山词霸

四、XML格式

需要先使用文档声明来声明XML文档
最简单的语法:
<?xml version=“1.0” encoding=“utf-8”?>
version 版本为1.0 (代表当前xml文档的版本)
encoding 编码格式 (编码格式utf-8、big5、gb2312)
standalone 是否独立(是否依靠dtd文件)  默认yes 
作用:告诉浏览器在解释时所采用的版本与格式等信息

浏览器中输出时指定类型:

<meta name="content-type" content="text/xml">

五、php操作XML

函数参考 --  XML 操作 --SimpleXML
SimpleXML 扩展提供了一个非常简单和易于使用的工具集,能将 XML 转换成一个带有一般属性选择器和数组迭代器的对象。

5.1读取xml:
simplexml_load_file()
attributes ()  获取属性
$xml  =  simplexml_load_file ( 'demo01.xml' );

[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- 这是xml-->  
  3. <libray>  
  4.   <book id="1" ccc="123">  
  5.     <title><三国演义></title>  
  6.     <author>罗贯中</author>  
  7.     <price>82</price>  
  8.   </book>  
  9.   <book id="2" ccc="456">  
  10.     <title><水浒传></title>  
  11.     <author>施耐庵</author>  
  12.     <price>78</price>  
  13.   </book>  
  14. </libray>  



[php] view plain copy 在CODE上查看代码片派生到我的代码片
  1. header('Content-type:text/html;charset=utf8');  
  2. if ( file_exists ( 'demo01.xml' )) {  
  3.      $xml  =  simplexml_load_file ( 'demo01.xml' );  
  4.      print_r ( $xml );  
  5.      echo "<table>";  
  6.      echo "<tr><td>ID</td><td>书名</td><td>作者</td><td>单价</td></tr>";  
  7.      foreach($xml->book as $v){  
  8.        echo "<tr><td>" .$v->attributes()->ccc . "</td>";  
  9.        echo "<td>" .$v->title . "</td>";  
  10.        echo "<td>" .$v->author . "</td>";  
  11.        echo "<td>" .$v->price . "</td>";  
  12.        echo "</tr>";  
  13.      }  
  14.      echo "</table>";  
  15. else {  
  16.     exit'Failed to open test.xml.' );  
  17. }  


5.2生成xml:

$xml  = new  SimpleXMLElement ( $string );
header('content-type:text/xml;charset=utf-8');

[php] view plain copy 在CODE上查看代码片派生到我的代码片
  1. header('content-type:text/xml;charset=utf-8');  
  2. $a1array("id"=>1,"title"=>"三国演义","author"=>"罗贯中","price"=>80);  
  3. $a2array("id"=>2,"title"=>"水浒传","author"=>"施耐庵","price"=>78);  
  4. $arr = array($a1,$a2);  
  5. echo json_encode($arr);exit;  
  6. $str = "<?xml version='1.0' encoding='utf-8'?><libray>";  
  7. foreach($arr as $v){  
  8.     $str .="  
  9. <book id='{$v['id']}'>  
  10.     <title><{$v['title']}></title>  
  11.     <author>{$v['author']}</author>  
  12.     <price>{$v['price']}</price>  
  13.   </book>";  
  14. }  
  15. $str .="</libray>";  
  16. echo $str;  

0 0
原创粉丝点击