- 时间:2022-12-01 02:07 编辑: 来源: 阅读:
- 扫一扫,手机访问
摘要:PHP实现使用DOM将XML数据存入数组的方法示例
本文实例讲述了PHP实现使用DOM将XML数据存入数组的方法。分享给大家供大家参考,具体如下:
<?php
$doc = new DOMDocument('1.0','utf-8');
$doc->load("config.xml");
$roots=$doc->documentElement;//获取根节点也就是config(仅有一个)
$childs=$roots->childNodes;//获取根节点下所有子节点也就是 db smarty
for($i=0;$i<$childs->length;$i++){ //按照根节点下的子节点数量进行循环存入数组
$config_item=$childs->item($i); //具体获得db smarty
$configs[$config_item->nodeName]=array();//将db smarty这两个子节点的名称作为数据存入数组中
$items=$config_item->childNodes; //获得db smarty下的所有子节点
for($j=0;$j<$items->length;$j++){//按照db smarty下所有子节点数进行循环将db smarty下的子节点名与值以二维数组存入
$item=$items->item($j); //通过循环获取每个db smarty 下的子节点
$configs[$config_item->nodeName][$item->nodeName]=$item->nodeValue;
}//二维数组的书写
}
var_dump($configs);
[b]PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:[/b]
[b]在线XML/JSON互相转换工具:
[/b][url=http://tools.jb51.net/code/xmljson]http://tools.jb51.net/code/xmljson[/url]
[b]在线格式化XML/在线压缩XML:
[/b][url=http://tools.jb51.net/code/xmlformat]http://tools.jb51.net/code/xmlformat[/url]
[b]XML[/b][b]在线压缩/格式化工具:
[/b][url=http://tools.jb51.net/code/xml_format_compress]http://tools.jb51.net/code/xml_format_compress[/url]
[b]XML[/b][b]代码在线格式化美化工具:
[/b][url=http://tools.jb51.net/code/xmlcodeformat]http://tools.jb51.net/code/xmlcodeformat[/url]
更多关于PHP相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/234.htm]PHP针对XML文件操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/623.htm]PHP数组(Array)操作技巧大全[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》、《[url=http://www.1sucai.cn/Special/196.htm]PHP错误与异常处理方法总结[/url]》、《[url=http://www.1sucai.cn/Special/348.htm]PHP基本语法入门教程[/url]》、《[url=http://www.1sucai.cn/Special/43.htm]php面向对象程序设计入门教程[/url]》、《[url=http://www.1sucai.cn/Special/84.htm]php+mysql数据库操作入门教程[/url]》及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》
希望本文所述对大家PHP程序设计有所帮助。