xpath和dom、simpleXML的结合

Posted on Posted in php
Tips: 本文创建于2014年5月8日,已超过 2 年,内容或图片可能已经失效!

xpath与dom的结合
[code lang="php"]
//>>1.将xml转换成一个DOM树
$dom = new DOMDocument('1.0','utf-8');
$dom->load('./students.xml');
//>>2.创建xpath对象
$xpath = new DOMXPath($dom);
//>>3.通过xpath上的query方法执行xpath查询语句
$nameNodeList = $xpath->query('/students/student[age=27]/name'); //返回的是DOMNodeList
foreach($nameNodeList as $nameNode){
echo($nameNode->textContent);
echo('<hr/>');
}
[/code]
xpath 与simplexml的结合
[code lang="php"]
$root = simplexml_load_file('./students.xml');
$names = $root->xpath('/students/student/name'); //xpath方法的返回值是一个数组
foreach($names as $name){
echo($name);
echo('<hr/>');
}
[/code]

» 转载请注明来源:若我若鱼 » xpath和dom、simpleXML的结合

Leave a Reply

Your email address will not be published. Required fields are marked *

3 × three =