欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php读取xml实例代码

程序员文章站 2022-06-06 11:44:54
php文件 复制代码 代码如下:
php文件
复制代码 代码如下:

<?php
class xpathextension{
public static function getnodes($domdoc, $xpathstring) {
$xp = new domxpath($domdoc);
$xp->registernamespace('x', 'http://www.w3.org/1999/xhtml');
$xp->registernamespace('xhtml', 'http://www.w3.org/1999/xhtml');
$xp->registernamespace('i18n', 'http://apache.org/cocoon/i18n/2.1');
$ret = array();
$nodes = $xp->query($xpathstring);
foreach ($nodes as $node) {
array_push($ret, $node);
}
return $ret;
}
}
$domdoc = new domdocument();
$domdoc->load("x1.xml");
$xpathstring = "//xml/products/product/description";
$domnodelist = xpathextension::getnodes($domdoc, $xpathstring);
//echo count($domnodelist);
foreach($domnodelist as $domnode){
echo $domnode->nodevalue;
}
?>

xml
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<xml>
<products>
<product>
<description>
abcd
</description>
</product>
</products>
</xml>

上一篇: 有种

下一篇: 手机不行