首頁  >  文章  >  後端開發  >  PHP SimpleXMLElement:XML檔案解析與讀寫

PHP SimpleXMLElement:XML檔案解析與讀寫

WBOY
WBOY原創
2016-07-28 08:26:091101瀏覽

PHP保存XML文件

<code><span>$xml</span><span>=</span><span>new</span> SimpleXMLElement(<span>XML</span>数据);
        <span>$xml</span><span>-></span>asXML(文件名);</code>

PHP讀取XML文件

<code><span>XXX</span>=<span> simplexml_load_file(文件名);</span></code>


1.使用面向對象方式訪問XML

<code><span><?php</span><span><span>class</span><span>XmlTest</span>
{</span><span>private</span><span>$filename</span> = <span>"a.xml"</span>;
    <span>private</span><span>$isreading</span>=<span>false</span>;
    <span>private</span><span>$rxml</span>;

<span>//创建XML文件</span><span>public</span><span><span>function</span><span>writeTest</span><span>()</span>
{</span><span>$_data</span> = <span><<<data
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <data>A</data>
    <data>B</data>
    <data>C</data>
    <from>
            <name fristname='陈'>老四</name>
            <arg>30</arg>
     </from>
    <heading>Reminder</heading>
    <body>Don't forget the meeting!</body>
</root>
data;</span><span>$xml</span> = <span>new</span> SimpleXMLElement(<span>$_data</span>);
        <span>$xml</span>->asXML(<span>$this</span>->filename);
    }
    <span>//读取文件</span><span>private</span><span><span>function</span><span>readFile</span><span>()</span>
    {</span><span>if</span>(!<span>$this</span>->isreading)
        {
            <span>$this</span>->rxml= simplexml_load_file(<span>$this</span>->filename);
            <span>$this</span>->isreading=<span>true</span>;
        }
    }
    <span>//测试:遍历某一数组</span><span>public</span><span><span>function</span><span>readdata</span><span>()</span>
    {</span><span>$this</span>->readFile();
        <span>foreach</span> (<span>$this</span>->rxml->data <span>as</span><span>$x</span>) {
            <span>echo</span><span>"["</span>.<span>$x</span>.<span>"]"</span>;

        }
        <span>echo</span><span>"<br>"</span>;
    }
    <span>//测试 用数组下标读取数组某一项</span><span>public</span><span><span>function</span><span>readdata1</span><span>()</span>
    {</span><span>$this</span>->readFile();
        <span>echo</span><span>"读取第[1]个data数据:"</span>;
        <span>echo</span><span>$this</span>->rxml->data[<span>1</span>] .<span>"<br>"</span>;
    }
    <span>//测试:读取2级XML节点</span><span>public</span><span><span>function</span><span>readfrom</span><span>()</span>
    {</span><span>$this</span>->readFile();
        <span>echo</span><span>"读取from节点:"</span>;
        <span>echo</span><span>$this</span>->rxml->from->name .<span>','</span>;
        <span>echo</span><span>$this</span>->rxml->from->arg.<span>"<br>"</span>;
                <span>//读取name节点的fristname属性</span><span>echo</span><span>$this</span>->rxml->from->name[<span>'fristname'</span>].<span>"<br>"</span>;
    }
}


<span>$tmp</span> = <span>new</span> XmlTest();
<span>$tmp</span>->writeTest();
<span>$tmp</span>->readdata();
<span>$tmp</span>->readdata1();
<span>$tmp</span>->readfrom();

<span>?></span></code>

瀏覽器列印:

<code>[<span>A</span>][<span>B</span>][<span>C</span>]
读取第[1]个data数据:B
读取from数据:老四,30
陈</code>


2.使用xpath存取XML., 用路徑取得節點,區別:取得的節點都以陣列傳回,因此用陣列下標存取

<code><span>public</span><span><span>function</span><span>readdata_xpath</span><span>()</span> {</span><span>$this</span>->readFile();
        <span>$data</span>= <span>$this</span>->rxml->xpath(<span>'/root/data'</span>);
        <span>foreach</span> (<span>$data</span><span>as</span><span>$x</span>) {
            <span>echo</span><span>"["</span>.<span>$x</span>.<span>"]"</span>;

        }
        <span>echo</span><span>"<br>"</span>;
    }
    <span>public</span><span><span>function</span><span>readdata1_xpath</span><span>()</span>
    {</span><span>$this</span>->readFile();
        <span>$data1</span> =  <span>$this</span>->rxml->xpath(<span>'/root/data'</span>);
        <span>echo</span><span>"读取第[1]个data数据:"</span>;
        <span>echo</span><span>$data1</span>[<span>1</span>] .<span>"<br>"</span>;
    }
    <span>public</span><span><span>function</span><span>readfrom_xpath</span><span>()</span>
    {</span><span>$this</span>->readFile();
        <span>$name</span>= <span>$this</span>->rxml->xpath(<span>'/root/from/name'</span>);
        <span>$arg</span>= <span>$this</span>->rxml->xpath(<span>'/root/from/arg'</span>);
        <span>echo</span><span>"读取from数据:"</span>; 
        <span>echo</span><span>$name</span>[<span>0</span>] .<span>','</span>;
        <span>echo</span><span>$arg</span>[<span>0</span>].<span>"<br>"</span>;
        <span>//读取name节点的fristname属性</span><span>echo</span><span>$name</span>[<span>0</span>][<span>'fristname'</span>].<span>"<br>"</span>;
    }</code>
<code>$tmp->readdata_xpath();
$tmp->readdata1_xpath();
$tmp->readfrom_xpath();

<span>*****</span><span>*****</span><span>*****</span><span>***</span>
浏览器打印:
[<span>A</span>][<span>B</span>][<span>C</span>] 
读取第[1]个data数据:B 
读取from数据:老四,30 
陈</code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介紹了 PHP SimpleXMLElement:XML檔案解析和讀寫,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn