Maison > php教程 > php手册 > 探讨PHP simplexml详细使用方法

探讨PHP simplexml详细使用方法

WBOY
Libérer: 2016-06-13 11:08:58
original
789 Les gens l'ont consulté

我们今天要和大家一起讨论的内容是关于

Xml已经成为目前数据存储与传递的驱势,从去年的流行起来的Ajax来说,它有一个方法(ResponseXML)就是返回Xml对象,然后由Javascript来处理这个Xml对象。

显然服务器端的强悍编程语言PHP来说更需要这种技术的支持,较之流行的有Xml Parse, XmlReader, Xml Rpc…这里要探讨的并不是这些,而是一个已经集成于PHP simplexml。其使用方法比较简单,而且不需要加载其它的文件,及相关的配置。当然我用到最多的也是这个,相对于其它的php xml解析类来说还是更了解一些。

假如我们有一个这样的文件,

PHP XML处理XML代码如下
 

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><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></span></li>
<li><span class="tag"><span> </span><span class="tag-name">phplamp</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span class="tag"><span> </span><span class="tag-name">post</span><span class="tag">></span><span>   </span></span></li>
<li><span class="tag"><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> /title</span><span class="tag">></span><span>   </span></span></span></li>
<li class="alt"><span class="tag"><span> </span><span class="tag-name">details</span><span class="tag">></span><span>详细内容一</span><span class="tag"><span> /details</span><span class="tag">></span><span>   </span></span></span></li>
<li><span class="tag"><span> /post</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span class="tag"><span> </span><span class="tag-name">post</span><span class="tag">></span><span>   </span></span></li>
<li><span class="tag"><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> /title</span><span class="tag">></span><span>   </span></span></span></li>
<li class="alt"><span class="tag"><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></span></li>
<li><span class="tag"><span> /post</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span class="tag"><span> </span><span class="tag-name">post</span><span class="tag">></span><span>   </span></span></li>
<li><span class="tag"><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> /title</span><span class="tag">></span><span>   </span></span></span></li>
<li class="alt"><span class="tag"><span> </span><span class="tag-name">details</span><span class="tag">></span><span>详细内容三</span><span class="tag"><span> /details</span><span class="tag">></span><span>   </span></span></span></li>
<li><span class="tag"><span> /post</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span class="tag"><span> /phplamp</span><span class="tag">></span><span> </span></span></li>
</ol>
Copier après la connexion

现在需要用PHP simplexml方法将其所有数据解析出来。

PHP XML处理PHP代码如下
 

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php   </span></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>
</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 class="tag-name">br</span><span> </span><span class="tag">/></span><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 class="tag-name">br</span><span> </span><span class="tag">/></span><span>";   </span></span>
</li>
<li>
<span>echo "Details: " . $value-</span><span class="tag">></span><span>details <br>. "</span><span class="tag"><span> </span><span class="tag-name">br</span><span> </span><span class="tag">/></span><span class="tag"><span> </span><span class="tag-name">br</span><span> </span><span class="tag">/></span><span>";   </span></span></span>
</li>
<li class="alt"><span>}   </span></li>
<li>
<span class="tag">?></span><span>  </span>
</li>
</ol>
Copier après la connexion

以上就是PHP simplexml的具体使用方法介绍。


source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal