Home > Backend Development > PHP Tutorial > Discuss the detailed use of PHP simplexml_PHP tutorial

Discuss the detailed use of PHP simplexml_PHP tutorial

WBOY
Release: 2016-07-15 13:31:57
Original
930 people have browsed it

What we are going to discuss with you today is about

Xml has become the current driving force for data storage and delivery, starting from the popularity of Ajax last year. , it has a method (ResponseXML) that returns an Xml object, and then Javascript processes this Xml object.

Obviously, the powerful server-side programming language PHP needs the support of this technology. Compared with the more popular ones, there are Xml Parse, PHP simplexml. Its use is relatively simple, and there is no need to load other files and related configurations. Of course, this is the one I use the most, and I know it better than other PHP xml parsing classes.

Suppose we have such a file,

PHP XML processing XML code is as follows

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><</span><span> ?xml </span><span class="attribute">version</span><span>=</span><span class="attribute-value">"1.0"</span><span> </span><span class="attribute">encoding</span><span>=</span><span class="attribute-value">"utf-8"</span><span class="tag">?></span><span>   </span></span></li>
<li>
<span class="tag"><</span><span> </span><span class="tag-name">phplamp</span><span class="tag">></span><span>   </span>
</li>
<li class="alt">
<span class="tag"><</span><span> </span><span class="tag-name">post</span><span class="tag">></span><span>   </span>
</li>
<li>
<span class="tag"><</span><span> </span><span class="tag-name">title</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"1"</span><span class="tag">></span><span>PHP XML处理介绍一</span><span class="tag"><</span><span> /title</span><span class="tag">></span><span>   </span>
</li>
<li class="alt">
<span class="tag"><</span><span> </span><span class="tag-name">details</span><span class="tag">></span><span>详细内容一</span><span class="tag"><</span><span> /details</span><span class="tag">></span><span>   </span>
</li>
<li>
<span class="tag"><</span><span> /post</span><span class="tag">></span><span>   </span>
</li>
<li class="alt">
<span class="tag"><</span><span> </span><span class="tag-name">post</span><span class="tag">></span><span>   </span>
</li>
<li>
<span class="tag"><</span><span> </span><span class="tag-name">title</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"2"</span><span class="tag">></span><span>PHP XML处理介绍二</span><span class="tag"><</span><span> /title</span><span class="tag">></span><span>   </span>
</li>
<li class="alt">
<span class="tag"><</span><span> </span><span class="tag-name">details</span><span class="tag">></span><span>详细内容二</span><span class="tag"></</span><span class="tag-name">details</span><span class="tag">></span><span>   </span>
</li>
<li>
<span class="tag"><</span><span> /post</span><span class="tag">></span><span>   </span>
</li>
<li class="alt">
<span class="tag"><</span><span> </span><span class="tag-name">post</span><span class="tag">></span><span>   </span>
</li>
<li>
<span class="tag"><</span><span> </span><span class="tag-name">title</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"3"</span><span class="tag">></span><span>PHP XML处理介绍三</span><span class="tag"><</span><span> /title</span><span class="tag">></span><span>   </span>
</li>
<li class="alt">
<span class="tag"><</span><span> </span><span class="tag-name">details</span><span class="tag">></span><span>详细内容三</span><span class="tag"><</span><span> /details</span><span class="tag">></span><span>   </span>
</li>
<li>
<span class="tag"><</span><span> /post</span><span class="tag">></span><span>   </span>
</li>
<li class="alt">
<span class="tag"><</span><span> /phplamp</span><span class="tag">></span><span> </span>
</li>
</ol>
Copy after login

Now we need to use the PHP simplexml method to parse all its data .

PHP XML processing PHP code is as follows

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><</span><span> ?php   </span></span></li><li><span>/**   </span></li><li class="alt"><span>* 加载Xml文件   </span></li><li><span>*/   </span></li><li class="alt"><span>$</span><span class="attribute">xml</span><span> = </span><span class="attribute-value">simplexml_load_file</span><span>("me.xml");   </span></li><li><span>/**   </span></li><li class="alt"><span>* 如果Xml为字符串的话可以用下面这个<br />方法,后面的使用方法一样   </span></li><li><span>* $</span><span class="attribute">xml</span><span> = </span><span class="attribute-value">simplexml_load_string</span><span>   </span></li><li class="alt"><span>*/   </span></li><li><span>/**   </span></li><li class="alt"><span>* 遍历$xml对象   </span></li><li><span>*/   </span></li><li class="alt"><span>foreach ($xml as $</span><span class="attribute">key</span><span> =</span><span class="tag">></span><span> $value) {   </span></span></li>
<li><span>// 获取属性   </span></li>
<li class="alt">
<span>$</span><span class="attribute">attr</span><span> = $value-</span><span class="tag">></span><span>title-</span><span class="tag">></span><span>attributes();   </span>
</li>
<li>
<span>echo "Id: " . $attr['id'] . "</span><span class="tag"><</span><span> </span><span class="tag-name">br</span><span> </span><span class="tag">/></span><span>";   </span>
</li>
<li class="alt">
<span>echo "Title: " . $value-</span><span class="tag">></span><span>title . "</span><span class="tag"><</span><span> </span><span class="tag-name">br</span><span> </span><span class="tag">/></span><span>";   </span>
</li>
<li>
<span>echo "Details: " . $value-</span><span class="tag">></span><span>details <br>. "</span><span class="tag"><</span><span> </span><span class="tag-name">br</span><span> </span><span class="tag">/></span><span class="tag"><</span><span> </span><span class="tag-name">br</span><span> </span><span class="tag">/></span><span>";   </span>
</li>
<li class="alt"><span>}   </span></li>
<li>
<span class="tag">?></span><span>  </span>
</li>
</ol>
Copy after login

The above is an introduction to the specific use of PHP simplexml.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446181.htmlTechArticleWhat we are going to discuss with you today is that Xml has become the current driving force for data storage and transmission. As for Ajax, which became popular last year, it has a method (ResponseXM...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template