> 백엔드 개발 > PHP 튜토리얼 > 探讨PHP simplexml详细使用方法_PHP教程

探讨PHP simplexml详细使用方法_PHP教程

WBOY
풀어 주다: 2016-07-15 13:31:57
원래의
913명이 탐색했습니다.

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

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>
로그인 후 복사

现在需要用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>
로그인 후 복사

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


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446181.htmlTechArticle我们今天要和大家一起讨论的内容是关于 Xml已经成为目前数据存储与传递的驱势,从去年的流行起来的Ajax来说,它有一个方法(ResponseXM...
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿