php读取xml实例代码
php读取xml实例代码
发布时间:2016-12-29 来源:查字典编辑
摘要:php文件复制代码代码如下:nodeValue;}?>xml复制代码代码如下:abcd

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教程学习
热门php教程学习
编程开发子分类