PHP saves XML files
<code><span>$xml</span><span>=</span><span>new</span> SimpleXMLElement(<span>XML</span>数据); <span>$xml</span><span>-></span>asXML(文件名);</code>
PHP reads XML files
<code><span>XXX</span>=<span> simplexml_load_file(文件名);</span></code>
1. Use object-oriented approach to access 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>
Browser printing:
<code>[<span>A</span>][<span>B</span>][<span>C</span>] 读取第[1]个data数据:B 读取from数据:老四,30 陈</code>
2. Use xpath to access XML., use path Get nodes, difference: The obtained nodes are returned as arrays, so use array subscripts to access
<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>
The above has introduced PHP SimpleXMLElement: XML file parsing, reading and writing, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.