php读取xml实例代码_php技巧
程序员文章站
2022-04-25 14:33:40
...
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
abcd
复制代码 代码如下:
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
复制代码 代码如下:
abcd
推荐阅读
-
微信公众平台开发实例 PHP开发 代码挂载SAE平台(六)星座物语 p
-
PHP在字符串中查找指定字符串并删除的代码_php技巧
-
PHP学习笔记 (1) 环境配置与代码调试_php技巧
-
PHP Session 变量的使用方法详解与实例代码_php技巧
-
php的SimpleXML方法读写XML接口文件实例解析_PHP
-
PHP屏蔽蜘蛛访问代码及常用搜索引擎的HTTP_USER_AGENT_php技巧
-
php ajax异步读取rss文档数据_php技巧
-
PHP读取网页文件内容的实现代码(fopen,curl等)_PHP教程
-
PHP分多步骤填写发布信息的简单方法实例代码_php实例
-
php截取后台登陆密码的代码_php技巧