php中操作xml文檔程式碼

來源:互聯網
上載者:User
 代碼如下 複製代碼

 

/*
<?xml version="1.0" encoding="UTF-8"?>
<班級>
<學生 number="101">
<名字>孫悟空</名字>
<名字>孫行者</名字>
<年齡>猴精猴精</年齡>
<介紹></介紹>
</學生>
<學生 number="102">
<名字>白骨精</名字>
<年齡>140</年齡>
<介紹>幻化萬千</介紹>
</學生>
<學生 number="103">
<名字>豬八戒</名字>
<名字>豬無能</名字>
<年齡>200</年齡>
<介紹>能吃會睡</介紹>
</學生>
</班級>
*/

class xmlDom{
    public $version;
    public $encoding;
    private $xml;
    private $items;
    private $seachNode = '';
    private $seachItem = '';
    private $seachValue = '';
    public $writeBytes = 0;

    function __construct($xmlFile ='', $version ='1.0', $encoding = 'UTF-8'){
        $this->version = $version;
        $this->encoding = $encoding;
        $this->xml = new DOMDocument($version, $encoding);
        if($xmlFile)$this->xml->load($xmlFile);
    }
    function getRootEle($rootTag){
        $this->xmlRoot = $this->xml->getElementsByTagName($rootTag)->item(0);
    }
    function getSeachItem($itemsTag, $seachNode, $seachValue){
        $this->items = $this->xml->getElementsByTagName($itemsTag);
        $this->items->length;
        for($i=0; $i<$this->items->length; $i++){
            $item = $this->items->item($i);//元素
            $node = $item->getElementsByTagName($seachNode);//節點
            for($j = 0; $j< $node->length; $j++){
                $subNode = $node->item($j);
                if($seachValue == $subNode->nodeValue){
                    $this->seachNode = $subNode;
                    $this->seachItem = $item;
                    $this->seachValue = $subNode->nodeValue;
                    break(2);
                }
            }
        }
        return ($this->seachNode) ? true : false;
    }
   
    function update($nodeValue, $nodeTag = '',$append = false, $index = 0){
        if($append){
            if($nodeTag)
                $this->seachItem->getElementsByTagName($nodeTag)->item($index)->nodeValue += $nodeValue;
            else
                $this->seachNode->nodeValue += $nodeValue;
        }else{
            if($nodeTag)
                $this->seachItem->getElementsByTagName($nodeTag)->item($index)->nodeValue = $nodeValue;
            else
                $this->seachNode->nodeValue = $nodeValue;
        }
    }
   
    function save($filename){
        $this->writeBytes = $this->xml->save($filename);
        return ($this->writeBytes) ? true : false;
    }
}

$test = new xmlDom('student.xml');
$test->getSeachItem('學生','年齡','103');//找到 年齡=103 的豬八戒
$test->update('小豬豬', '名字', false, 1); //把豬八戒的第二個名字改成:小豬豬
$test->save('new.xml'); //儲存成新檔案

上面是使用了dom來操作,下面我們利用php中的SimpleXML來操作xml,也算是很標準的一個操作xml文檔的類了。

simplexml_load_file(string filename)
這裡的 filename變數是用於儲存 XML資料檔案的檔案名稱及其所在路徑。以下代碼使用 simplexml_load_file函數來建立了一個 SimpleXML對象。

 代碼如下 複製代碼

<?php
$xml = simplexml_load_file(’example.xml’); //建立 SimpleXML對象
print_r($xml);         //輸出 XML
?>

其中,example.xml儲存的資料與上面的$data完全相同,運行結果也與上面完全相同。
上面兩種方法實現了同樣的功能,其區別就在於 XML的資料來源不同。如果 XML的資料來源在 PHP指令檔中,則需要使用 simplexml_load_string來進行建立。如果 XML的資料來源在一個單獨的 XML檔案中,則需要使用 simplexml_load_file來進行建立。

讀取 XML資料中的標籤
與運算元群組類型的變數類似,讀取 XML也可以通過類似的方法來完成。例如,如果需要讀取上面 XML資料中每一個“ depart”標籤下的“name”屬性,可以通過使用 foreach函數來完成,如以下代碼
所示。

 代碼如下 複製代碼

<?php $xml = simplexml_load_file(’example.xml’); foreach($xml->depart as $a)
{
echo “$a->name <BR>”;
}
?>

運行結果如下所示。
production support
testing center
//讀取 XML檔案 //迴圈讀取 XML資料中的每一個 depart標籤
//輸出其中的 name屬性
也可以使用方括弧“ []”來直接讀取 XML資料中指定的標籤。以下代碼輸出了上面 XML資料中的第一個“depart”標籤的“name”屬性。

 

 代碼如下 複製代碼
<?php
$xml = simplexml_load_file(’example.xml’); //讀取 XML檔案
echo $xml->depart->name[0]; //輸出節點
?>

運行結果如下所示。
production support
對於一個標籤下的所有子標籤,SimpleXML組件提供了 children方法進行讀取。例如,對於上面的 XML資料中的“ depart”標籤,其下包括兩個子標籤:“ name”和“employees”。以下代碼實現了對第一個“depart”標籤下的子標籤的讀取。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.