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>
复制代码 代码如下:
<?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>