php解析XML的样例及参考

本文部分参考一下链接

php解析样例:http://babyjoycry.javaeye.com/blog/598732

php获取xml attribute:http://www.w3schools.com/dom/dom_nodes_get.asp

php关于xml的参考资料:http://php.net/manual/en/book.domxml.php

 

 

Php代码 
  1. <?php  
  2. $xml ="1.xml";  
  3. $dom = new DOMDocument();  
  4. $dom->load($xml);  
  5. $root = $dom->documentElement;  
  6. $arr=array();  
  7. foreach ($root->childNodes as $item)  
  8. {  
  9.     if($item->hasChildNodes()){  
  10.   
  11. $tmp=array();  
  12.     foreach($item->childNodes as $one){  
  13.         if(!emptyempty($one->tagName)){  
  14.         $tmp[$one->tagName]=$one->nodeValue;  
  15.         }  
  16.     }  
  17.     $arr[$item->tagName]=$tmp;  
  18.     }  
  19. }  
  20. print_r($arr);  


1,xml文件如下: 
Xml代码 
  1. <all>  
  2. <杭州>  
  3.         <拼车顺风车>http%3A%2F%2Fhz.fenlei168.com%2Fpinche%2F</拼车顺风车>  
  4.         <四S店经销商>http%3A%2F%2Fhz.fenlei168.com%2Fssss%2F</四S店经销商>  
  5.         <二手汽车>http%3A%2F%2Fhz.fenlei168.com%2Fershouche%2F</二手汽车>  
  6.         <汽车配件>http%3A%2F%2Fhz.fenlei168.com%2Fqipeijian%2F</汽车配件>  
  7.         <租车代驾陪练>http%3A%2F%2Fhz.fenlei168.com%2Fdaijia%2F</租车代驾陪练>  
  8.         <汽车服务过户>http%3A%2F%2Fhz.fenlei168.com%2Fqicheguohu%2F</汽车服务过户>  
  9.         <二手摩托车>http%3A%2F%2Fhz.fenlei168.com%2Fershoumotuo%2F</二手摩托车>  
  10.         <自行车电动车>http%3A%2F%2Fhz.fenlei168.com%2Fzixingche%2F</自行车电动车>  
  11.         <其它车辆信息>http%3A%2F%2Fhz.fenlei168.com%2Fqitache%2F</其它车辆信息>  
  12. </杭州>  
  13. <武汉>  
  14.         <拼车顺风车>http%3A%2F%2Fwh.fenlei168.com%2Fpinche%2F</拼车顺风车>  
  15.         <四S店经销商>http%3A%2F%2Fwh.fenlei168.com%2Fssss%2F</四S店经销商>  
  16.         <二手汽车>http%3A%2F%2Fwh.fenlei168.com%2Fershouche%2F</二手汽车>  
  17.         <汽车配件>http%3A%2F%2Fwh.fenlei168.com%2Fqipeijian%2F</汽车配件>  
  18.         <租车代驾陪练>http%3A%2F%2Fwh.fenlei168.com%2Fdaijia%2F</租车代驾陪练>  
  19.         <汽车服务过户>http%3A%2F%2Fwh.fenlei168.com%2Fqicheguohu%2F</汽车服务过户>  
  20.         <二手摩托车>http%3A%2F%2Fwh.fenlei168.com%2Fershoumotuo%2F</二手摩托车>  
  21.         <自行车电动车>http%3A%2F%2Fwh.fenlei168.com%2Fzixingche%2F</自行车电动车>  
  22.         <其它车辆信息>http%3A%2F%2Fwh.fenlei168.com%2Fqitache%2F</其它车辆信息>  
  23. </武汉>  
  24. </all>  


以上程序会将1.xml解析,并将内容放入数组。

Get the Value of an Attribute

In the DOM, attributes are nodes. Unlike element nodes, attribute nodes have text values.

The way to get the value of an attribute, is to get its text value.

This can be done using the getAttribute() method or using the nodeValue property of the attribute node.


Get an Attribute Value - getAttribute()

The getAttribute() method returns an attribute value.

The following code retrieves the text value of the "lang" attribute of the first <title> element:

Example

xmlDoc=loadXMLDoc("books.xml");

txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("lang");

Try it yourself »

Result:  txt = "en"

Example explained:

  1. Load "books.xml" into xmlDoc using loadXMLDoc()
  2. Set the txt variable to be the value of the "lang" attribute of the first title element node

Loop through all <book> elements and get their "category" attributes: Try it yourself

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值