• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

php操作xml

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

最近计划写个人的小网站,一系列原因选择了用php来写,最大的问题就是虽然php很流行,但我从来没有接触过php,看了一个多星期的基本语法后做些小练习热热身,但是期间是各种问题啊,主要是对php不熟悉,遇到一些总结一些吧。

数据

<?xml version="1.0"?>
<books>
    <book name="JavaScript: The Defiitive Guide" publisher="O'Reilly Media, Inc.">
        <author>David Flanagan</author>
    </book>
    <book name="PHP anf MySQL Web Development" publisher="Perason Education">
        <author>Luke Welling</author>
        <author>Laura Thomson</author>
    </book>
    <book name="HTTP: The Defiitive Guide" publisher="O'Reilly Media, Inc.">
        <author>David Courley</author>
        <author>Brian Totty</author>
    </book>
</books>

 

XML几个基本概念

节点:节点也就是很多程序语言中处理XML时的Node,节点是一个比较宽泛的概念,在XML中元素,属性,名字空间,注释,文本内容,处理指令,还有整个文档都属于节点,也就是说XML文档中每个独立的一小部分都是节点,<books></books>是,<?xml version=”1.0”?>也是,name=”XXXX”也是,<author></author>标签是,甚至作者的名字David Flanagan都是一个文本节点。

 

元素:很多程序语言都有对XML处理,节点是一个很宽泛的概念,因为要统一API,对节点不会有过多方法,而元素也就是Element是节点的一个子集,简单讲就是<xxx></xxx>这样的标签才算,一般会有很多针对元素的操作方法。

 

属性:这个比较好理解,在<>里面的类似XX=”OO”等东西都是属性节点

 

转义字符:和HTML等类似,xml也有语言占用的符号,想使用的这些特殊字符的时候需要转义

 

<

&lt;

>

&gt;

&

&amp;

&apos;

&quot;

 

DOMDocument对象

我使用的是DOMDocument对象来操作xml,感觉用起来比simpleXml科学一些,当然第一天使用php,纯属个人感觉。DOMDocument有几个常用的属性和方法。

属性 作用
attributes 节点属性集合
parentNode
节点父节点
documentElement 文档根节点
nodeName 节点的名字
nodeType 节点类型
nodeValue 节点值
Text 节点及其子节点转换为文字

 

 

方法 作用
appendChild 为节点添加子节点
createAttribute 创建属性节点
createElement 创建元素
getElementsByTagName 通过节点名获取节点集合
hasChildNodes 判断节点是否有子节点
insertBefore 在节点
Load 通过文档路径加载xml
loadXML 加载zml字符串
removeChild 删除子节点
removeAttribute 删除属性节点
save 保存文档

 

加载xml

 

$path=$_SERVER["DOCUMENT_ROOT"].'/books.xml';
    $books=new DOMDocument();
    $books->load($path);

读取/遍历节点与属性

 

 

$bookElements=$books->getElementsByTagName('book');

    foreach($bookElements as $book){
        foreach ($book->attributes as $attr) {
            echo strtoupper($attr->nodeName).' —— '.$attr->nodeValue.'<br/>';
        }
        echo "AUTHOR: ";
        foreach ($book->getElementsByTagName('author') as $author) {
            echo $author->nodeValue.'&emsp;';
        }
        echo '<br/><br/>';
    }

 

 

当然对于很多属性,只想读一个,可以通过item(index)方法按索引读取

echo $book->attributes->item(1)->nodeValue;

 

还可以通过强大的xpath查询

$xpath = new domxpath($books);
$bookElements=$xpath->query("/books/book");

 

修改属性/节点

 

foreach($bookElements as $book){
        foreach ($book->attributes as $attr) {
            #$book->setAttribute($attr->nodeName,strtoupper($attr->nodeValue));
            $attr->nodeValue=strtoupper($attr->nodeValue);
        }
        echo "AUTHOR: ";
        foreach ($book->getElementsByTagName('author') as $author) {
            $author->nodeValue=strtoupper($author->nodeValue);
        }

    }
    $books->save($path);

 

对属性修改可以直接访问其nodeValue改动,也可以使用setAttribute方法,改动完了别忘了使用save保存。

$book->setAttribute($attr->nodeName,strtoupper($attr->nodeValue));
$attr->nodeValue=strtoupper($attr->nodeValue);

 

添加元素/属性

 

$newBook=$books->createElement('book'); #创建新元素
    $newBook->setAttribute('name','PHP Objects, Patterns, and Practice');#创建新属性,方法一

    $publisher=$books->createAttribute('publisher');#创建新属性,方法二
    $publisher->nodeValue='Apress L.P';
    $newBook->appendChild($publisher); #把属性添加到元素上

    $author=$books->createElement('author');#创建子元素
    $author->nodeValue='Matt Zandstra';
    $newBook->appendChild($author);#把子元素添加到父元素上

    $books->documentElement->appendChild($newBook);#添加整个节点
    $books->save($path);

 

删除属性/节点

 

$first=$bookElements->item(0);
    $first->removeAttribute('publisher');

    $second=$bookElements->item(1);
    $second->parentNode->removeChild($second);

    $books->save($path);

 

最后

 

初学php文章肯定有很多谬误,希望大家批评指正,共同进步。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
./upload/index.php发布时间:2022-07-10
下一篇:
为IIS增加PHP支持发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap