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>
객체 지향 접근 방식을 사용하여 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. 경로를 사용하여 노드를 가져옵니다. array 이므로
<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>
위에서는 PHP SimpleXMLElement: XML 파일 구문 분석, 읽기 및 쓰기에 대한 내용을 소개했습니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.