首頁 > 後端開發 > php教程 > php中的XML DOM(10)

php中的XML DOM(10)

WBOY
發布: 2016-08-08 09:27:42
原創
1025 人瀏覽過

1.PHP DOM (1)

Php中的DOM跟javascript不一樣,屬性不用另外增加一個節點

2.主要類

     DOMDocument :文檔類別

     DOMNodeList :節點列表類別

     DOMNode :節點類別

     DOMElement :元素類別

3. DOMDocument類別

  3.1建立文件物件

     DOMDocument::__construct ([ string $version [, string $encoding ]] )

     [ string $version :版本號

     [, string $encoding ]]:字元集

  3.2、載入xml檔

     mixed DOMDocument::load ( string $filename)

     string $filename:要載入的xml檔名

  3.3、取得節點

     DOMNodeList DOMDocument::getElementsByTagName ( string name )

     String name:要取得的節點名稱

範例:Demo.php

<?<span>php
    header ( </span><span>'</span><span>Content-Type:text/html;charset=gb2312</span><span>'</span><span> );
</span><span>//</span><span> 利用php解析demo01.xml
</span><span>//</span><span> 实例化dom对象</span>
    $dom = <span>new</span><span> DOMDocument ();
</span><span>//</span><span> 加载xml文件</span>
    $dom->load ( <span>'</span><span>demo01.xml</span><span>'</span><span> );
</span><span>//</span><span> 通过dom对象获取person节点,注意:这里返回值是一个DOMNodeList类对象</span>
    $persons = $dom->getElementsByTagName ( <span>'</span><span>person</span><span>'</span><span> );
</span><span>//</span><span> 共有几个person</span>
    echo <span>'</span><span>共有</span><span>'</span> . $persons->length . <span>'</span><span>个人<br></span><span>'</span>;      <span>//</span><span>输出共有两个人
</span><span>//</span><span> 选中索引为0的person,也就是第一个人</span>
    $person = $persons->item ( <span>0</span><span> );
</span><span>//</span><span> 在这个person下获取name节点,返回的仍然是一个DOMNodeList类对象</span>
    $names = $person->getElementsByTagName ( <span>'</span><span>name</span><span>'</span><span> );    
</span><span>//</span><span> 输出姓名</span>
echo $names->item ( <span>0</span> )->nodeValue . <span>'</span><span><br></span><span>'</span>;       <span>//</span><span>输出'张三'</span>
为什么要进行两次取的Nodelist呢?请参照上图,因为有两个节点
登入後複製

demo.xml

<?xml version=<span>"</span><span>1.0</span><span>"</span> encoding=<span>"</span><span>UTF-8</span><span>"</span>?>
<persons>
    <person id=<span>"</span><span>s101</span><span>"</span>>
        <name>zhangsan</name>
        <age><span>30</span></age>
    </person>
    <person>
        <name>wangwu</name>
        <age><span>18</span></age>
    </person>
</persons>
登入後複製

4. DOMNodeList類別 (上面的person是NodeList節點)

  1、Length

     在目前節點清單中共有幾個節點

  2、DOMElement DOMNodelist::item ( int $index )

     選取索引為index的節點

     int $index:索引

5、 DOMNode類

     1、nodeValue 節點的值

6、 關於屬性的讀取

   1、bool DOMElement::hasAttribute ( string $name )

      判斷是否具有某一屬性(只判斷某屬性下有沒有)

      String $name:屬性名稱

   2、bool DOMNode::hasAttributes ( void )

      判斷是否有屬性(只要有屬性就回傳true)

   3、string DOMElement::getAttribute ( string $name )

      取得指定屬性的屬性值

String $name:屬性名稱

範例2.用連貫運算輸出

<?<span>php
    $dom </span>= <span>new</span> DOMDocument ( <span>'</span><span>1.0</span><span>'</span>, <span>'</span><span>utf-8</span><span>'</span><span> );
    $dom</span>->load ( <span>'</span><span>demo01.xml</span><span>'</span><span> );
</span><span>//</span><span> 想得到第一个人的姓名</span>
    echo $dom->getElementsByTagName ( <span>'</span><span>person</span><span>'</span> )->item ( <span>0</span><span> )
    </span>->getElementsByTagName ( <span>'</span><span>name</span><span>'</span> )->item ( <span>0</span> )-><span>nodeValue;
输出:张三</span>
登入後複製

Example3:

<?<span>php
    header ( </span><span>'</span><span>Content-Type:text/html;charset=gb2312</span><span>'</span><span> );
    $dom </span>= <span>new</span> DOMDocument ( <span>'</span><span>1.0</span><span>'</span>, <span>'</span><span>utf-8</span><span>'</span><span> );
    $dom</span>->load ( <span>'</span><span>demo01.xml</span><span>'</span><span> );
    $person </span>= $dom->getElementsByTagName ( <span>'</span><span>person</span><span>'</span> )->item ( <span>0</span><span> );
</span><span>//</span><span> 判断节点是否具有id属性</span>
    <span>if</span> ($person->hasAttribute ( <span>'</span><span>id</span><span>'</span><span> )) {
        echo </span><span>'</span><span>具有id属性</span><span>'</span><span>;
    } </span><span>else</span><span> {
        echo </span><span>'</span><span>没有id属性</span><span>'</span><span>;
    }
    echo </span><span>'</span><span><br></span><span>'</span><span>;
</span><span>//</span><span> 判断节点是否具有属性</span>
    <span>if</span> ($person-><span>hasAttributes ()) {
        echo </span><span>'</span><span>有属性</span><span>'</span><span>;
    } </span><span>else</span><span> {
        echo </span><span>'</span><span>没有任何属性</span><span>'</span><span>;
    }
    echo </span><span>'</span><span><br></span><span>'</span><span>;
</span><span>//</span><span> 得到第一个person的id属性值</span>
    echo $person->getAttribute ( <span>'</span><span>id</span><span>'</span> );
登入後複製

之前的例子只能得到一條數據,現在我想得到所有人的完整訊息,該如何實現?

範例4:

如果這種有屬性的,也要遍歷出來呢?

<?<span>php
    header ( </span><span>'</span><span>content-type:text/html;charset=gb2312</span><span>'</span><span> );
    $dom </span>= <span>new</span> DOMDocument ( <span>'</span><span>1.0</span><span>'</span>, <span>'</span><span>utf-8</span><span>'</span><span> );
    $dom</span>->load ( <span>'</span><span>demo01.xml</span><span>'</span><span> );
</span><span>//</span><span> 得到所有person节点</span>
    $persons = $dom->getElementsByTagName ( <span>'</span><span>person</span><span>'</span><span> );
</span><span>//</span><span> 得到总人数</span>
    $count = $persons-><span>length;
    </span><span>for</span>($i = <span>0</span>; $i < $count; $i ++<span>) {
    </span><span>//</span><span> $person表示第i个人</span>
        $person = $persons-><span>item ( $i );
        echo </span><span>'</span><span>第</span><span>'</span> . ($i + <span>1</span>) . <span>'</span><span>个人的</span><span>'</span><span>;
        </span><span>if</span> ($person->hasAttribute ( <span>'</span><span>id</span><span>'</span><span> )) {
            echo </span><span>'</span><span>学号:</span><span>'</span> . $person->getAttribute ( <span>'</span><span>id</span><span>'</span><span> );
        }
        echo </span><span>'</span><span>姓名:</span><span>'</span> . $person->getElementsByTagName ( <span>'</span><span>name</span><span>'</span> )->item ( <span>0</span> )-><span>nodeValue;
        echo </span><span>'</span><span>年龄:</span><span>'</span> . $person->getElementsByTagName ( <span>'</span><span>age</span><span>'</span> )->item ( <span>0</span> )-><span>nodeValue;
        echo </span><span>'</span><span><br></span><span>'</span><span>;
    }
</span>?>
登入後複製

以上就介紹了php中的XML DOM(10),包含了面向的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板