php解析XML的样例及参考
来源:互联网 发布:ubuntu 16.04 wingide 编辑:程序博客网 时间:2024/06/10 18:59
本文部分参考一下链接
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 解析xml文件(不需要下载其他类库) 将数据解析到数组
文章分类:PHP编程- <?php
- $xml ="1.xml";
- $dom = new DOMDocument();
- $dom->load($xml);
- $root = $dom->documentElement;
- $arr=array();
- foreach ($root->childNodes as $item)
- {
- if($item->hasChildNodes()){
- $tmp=array();
- foreach($item->childNodes as $one){
- if(!emptyempty($one->tagName)){
- $tmp[$one->tagName]=$one->nodeValue;
- }
- }
- $arr[$item->tagName]=$tmp;
- }
- }
- print_r($arr);
1,xml文件如下:
- <all>
- <杭州>
- <拼车顺风车>http%3A%2F%2Fhz.fenlei168.com%2Fpinche%2F</拼车顺风车>
- <四S店经销商>http%3A%2F%2Fhz.fenlei168.com%2Fssss%2F</四S店经销商>
- <二手汽车>http%3A%2F%2Fhz.fenlei168.com%2Fershouche%2F</二手汽车>
- <汽车配件>http%3A%2F%2Fhz.fenlei168.com%2Fqipeijian%2F</汽车配件>
- <租车代驾陪练>http%3A%2F%2Fhz.fenlei168.com%2Fdaijia%2F</租车代驾陪练>
- <汽车服务过户>http%3A%2F%2Fhz.fenlei168.com%2Fqicheguohu%2F</汽车服务过户>
- <二手摩托车>http%3A%2F%2Fhz.fenlei168.com%2Fershoumotuo%2F</二手摩托车>
- <自行车电动车>http%3A%2F%2Fhz.fenlei168.com%2Fzixingche%2F</自行车电动车>
- <其它车辆信息>http%3A%2F%2Fhz.fenlei168.com%2Fqitache%2F</其它车辆信息>
- </杭州>
- <武汉>
- <拼车顺风车>http%3A%2F%2Fwh.fenlei168.com%2Fpinche%2F</拼车顺风车>
- <四S店经销商>http%3A%2F%2Fwh.fenlei168.com%2Fssss%2F</四S店经销商>
- <二手汽车>http%3A%2F%2Fwh.fenlei168.com%2Fershouche%2F</二手汽车>
- <汽车配件>http%3A%2F%2Fwh.fenlei168.com%2Fqipeijian%2F</汽车配件>
- <租车代驾陪练>http%3A%2F%2Fwh.fenlei168.com%2Fdaijia%2F</租车代驾陪练>
- <汽车服务过户>http%3A%2F%2Fwh.fenlei168.com%2Fqicheguohu%2F</汽车服务过户>
- <二手摩托车>http%3A%2F%2Fwh.fenlei168.com%2Fershoumotuo%2F</二手摩托车>
- <自行车电动车>http%3A%2F%2Fwh.fenlei168.com%2Fzixingche%2F</自行车电动车>
- <其它车辆信息>http%3A%2F%2Fwh.fenlei168.com%2Fqitache%2F</其它车辆信息>
- </武汉>
- </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:
- Load "books.xml" into xmlDoc using loadXMLDoc()
- 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
- php解析XML的样例及参考
- php的xml解析 simplexml_load_string
- php对XML的解析
- PHP解析xml的方法
- php对xml的解析-读xml
- XML的创建及解析
- python解析和生产xml的一些参考
- xml及DTD的解析及运用
- php解析带CDATA的XML
- PHP解析XML的四种方法
- php 解析xml 的四种方法
- php对xml的解析方法
- PHP XML操作的各种方法解析
- php 解析xml 的四种方法
- PHP XML操作的各种方法解析
- php解析xml的四种方法
- php解析xml 的几种方法
- PHP 解析有命名空间的 XML
- 亲身经历:月薪从800元-1万5千元,我用了6年时间。
- C#多显示器转换的两种方法——SetWindowPos,Screen
- 黑莓模拟器与电脑互发邮件
- C++中使用Socket通信包定义和读取方法
- 040_《Delphi函数参考大全》
- php解析XML的样例及参考
- Linux环境下的C/C++基础调试技术1——初步了解
- delphi
- 面向对象更是一种方法论
- ORACLE中null的排序问题
- 无题
- 图灵2010.12书讯
- Wine下完美安装QQ 2010
- boost 1.45.0 的安装及测试(Visual Studio 2010)