Heim > Backend-Entwicklung > PHP-Tutorial > PHP SimpleXMLElement:XML文件解析和读写

PHP SimpleXMLElement:XML文件解析和读写

WBOY
Freigeben: 2016-07-28 08:26:09
Original
1140 Leute haben es durchsucht

PHP保存XML文件

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

PHP读取XML文件

<code><span>XXX</span>=<span> simplexml_load_file(文件名);</span></code>
Nach dem Login kopieren


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>
<root>
    <data>A</data>
    <data>B</data>
    <data>C</data>
    <from>
            <name fristname="陈">老四</name>
            <arg>30</arg>
     </from>
    <heading>Reminder</heading>
    Don't forget the meeting!
</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></span></code>
Nach dem Login kopieren

浏览器打印:

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


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>
Nach dem Login kopieren
<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>
Nach dem Login kopieren
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了 PHP SimpleXMLElement:XML文件解析和读写,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage