Heim > Artikel > Backend-Entwicklung > PHP5对象simplexml的详细分析_PHP教程
simplexml_load_file(str file); 将一个xml文档载入一个simplexml对象中,此方法返回一个句柄
simplexml_load_string(str string);将一段xml文档载入一个simplexml对象中,此方法返回一个句柄
simplexml_import_dom(data) 该函数把一个dom节点转为simplexml对象,其中data为实用的DOM节点
$simplexml->addAttribute(name,value) 给simplexml对象元素添加一个属性,注意,是simplexml对象元素,不是simplexml对象句柄。
$simplexml->addChlid(name,value)给指定的XML节点添加一个子节点
$simplexml->asXML() 从SimpleXMLElement对象中以一个字符串的形式返回XML文档,可以当作保存xml文档来来用
$simplexml->attributes() 返回由PHP5对象simplexml元素节点的属性组成的数组,
运行上面代码会打印出以个数组,Array ( [@attributes] => Array ( [name] => cx,html [tt] => ddd ) )
可见,$simplexml->attributes()得到的是一个xml元素节点的所有属性,不过被包含在一个[@attributes]数组中,所以要通过$xml = $xml['@attributes']来获得
$simplexml->Children() 返回simplexml对象元素节点的孩子组成的数组
$simplexml->__construct() 创建一个新的XML文档
$simplexml->getDocNamespaces() 该函数返回$simplexml对象定义的命名空间
$simplexml->getName() 返回$simplexml对象元素的名称,即标签名
$simplexml->getNamespaces() 返回$simplexml对象使用的命名空间
$simplexml->registerXpathNamespace() 该函数为下一次XPATH查询创建命名空间语境
$simplexml->xpath() 使用xpath的语法来解析一个PHP5对象simplexml
实例代码1
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">xml</span></font></strong><span> </span><span class="attribute"><font color="#ff0000">version</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"1.0"</font></span><span> </span><span class="attribute"><font color="#ff0000">encoding</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"gbk"</font></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span></span></li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">LeapsoulXML</span><span class="tag">></span></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">LeapsoulInfo</span><span class="tag">></span></span></font></strong><span> </span> </li> <li class=""> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">name</span><span class="tag">></span></span></font></strong><span>Leapsoul-PHP网站开发</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">name</span><span class="tag">></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">website</span><span class="tag">></span></span></font></strong><span>http://www.leapsoul.cn</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">website</span><span class="tag">></span></font></strong><span> </span> </li> <li class=""> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">description</span><span class="tag">></span></span></font></strong><span>分享PHP网站开发与建设的乐趣,教你如何建立网站</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">description</span><span class="tag">></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">bloger</span><span class="tag">></span></span></font></strong><span>David</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">bloger</span><span class="tag">></span></font></strong><span> </span> </li> <li class=""> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">date</span><span class="tag">></span></span></font></strong><span>2009-05-13</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">date</span><span class="tag">></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">qq</span><span class="tag">></span></span></font></strong><span>QQ:154130270</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">qq</span><span class="tag">></span></font></strong><span> </span> </li> <li class=""> <span> </span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">LeapsoulInfo</span><span class="tag">></span></font></strong><span> </span> </li> <li class="alt"><span> </span></li> <li class=""> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">LeapsoulInfo</span><span class="tag">></span></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">name</span><span class="tag">></span></span></font></strong><span>Leapsoul-PHP网站开发</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">name</span><span class="tag">></span></font></strong><span> </span> </li> <li class=""> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">website</span><span class="tag">></span></span></font></strong><span>http://www.leapsoul.cn</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">website</span><span class="tag">></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">description</span><span class="tag">></span></span></font></strong><span>分享PHP网站开发与建设的乐趣,教你如何建立网站</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">description</span><span class="tag">></span></font></strong><span> </span> </li> <li class=""> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">bloger</span><span class="tag">></span></span></font></strong><span>David</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">bloger</span><span class="tag">></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">date</span><span class="tag">></span></span></font></strong><span>2009-05-13</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">date</span><span class="tag">></span></font></strong><span> </span> </li> <li class=""> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">qq</span><span class="tag">></span></span></font></strong><span>QQ:154130270</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">qq</span><span class="tag">></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">LeapsoulInfo</span><span class="tag">></span></font></strong><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">LeapsoulXML</span><span class="tag">></span></font></strong><span> </span> </li> </ol>
我们可以结合上面的PHP5对象simplexml示例,再加上自己的了解,应该能够充分的认识这一新增的函数。