PHP封装的XML简单操作类完整实例
发布时间:2020-12-12 22:21:26 所属栏目:PHP教程 来源:网络整理
导读:本篇章节讲解PHP封装的XML简单操作类。供大家参考研究具体如下: xml_dom.php封装类文件: dbfile = $db_file; if(!file_exists($db_file)) {// die('未找到数据库文件'); $this->dblink = new DOMDocument('1.0','utf-8'); $root = $this->dblink
本篇章节讲解PHP封装的XML简单操作类。分享给大家供大家参考,具体如下: xml_dom.php封装类文件: dbfile = $db_file;
if(!file_exists($db_file))
{
// die('未找到数据库文件');
$this->dblink = new DOMDocument('1.0','utf-8');
$root = $this->dblink->createElement('root');
$this->dblink->appendChild($root);
$this->dblink->formatOutput = true; // xml文件保留缩进样式
$this->dblink->save($this->dbfile);
}
else
{
$this->dblink = new DOMDocument();
$this->dblink->formatOutput = true;
$this->dblink->load($this->dbfile);
}
}
/**
* 遍历所有元素
* ===============================================
* 标准xml文件,一个元素可能有n个属性,可用自定义键[nodevalue]获取元素值
*
*
简单用法示例如下: smp.xml文件: index.php文件: getData();//读取xml文件内容
var_dump($xmlarr);
运行结果:
array(3) {
[0]=>
array(3) {
["id"]=>
string(1) "1"
["title"]=>
string(9) "标题一"
["content"]=>
string(15) "详细内容一"
}
[1]=>
array(3) {
["id"]=>
string(1) "2"
["title"]=>
string(9) "标题二"
["content"]=>
string(15) "详细内容二"
}
[2]=>
array(3) {
["id"]=>
string(1) "3"
["title"]=>
string(9) "标题三"
["content"]=>
string(15) "详细内容三"
}
}
}
PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用: 在线XML/JSON互相转换工具: 在线格式化XML/在线压缩XML: XML在线压缩/格式化工具:XML代码在线格式化美化工具:更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》及《》 希望本文所述对大家PHP程序设计有所帮助。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Longest Substring Without Repeating Characters 字符串中
- php – 调用$this-> load-> view()后停止执行
- php – 需要通过https加载http js和css – 得到mix_content
- php – 如何使用facebook-opengraph插件登录yii
- 如何使单个PHP语句的警告静音?
- php采用curl模仿登录人人网发布动态的方法
- PHP使用Get变量获取中文乱码的解决方法
- php die()与exit()的区别实例详解
- PHP MYSQL乱码问题,使用SET NAMES utf8校正
- PHP中遍历二维数组_以不同形式的输出操作实例