CodeIgniter自定义扩展-XML库
八月 3, 2009 by admin | 0 Comment »
这是用于CodeIgniter的XML扩展库,用来解析XML文档,修改里面的结点以及获取结点内容。原版来自:http://codeigniter.com/wiki/Xml_Library/,当然原版只有基本的解析XML功能,我对这个类进行了一点扩展,可以满足通常情况下的需要了。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | < ?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /** * 用于CodeIgniter的XML解析库 * * 提供XML文档哦解析和修改结点值的功能。 * 注意:此库仅适用于PHP5。 * @author: (原作者)Woody Gilk (注释及扩展): yinzhili * @version 1.0 * * 许可: http://creativecommons.org/licenses/by-sa/2.5/ * URL: http://codeigniter.com/wiki/Xml_Library/ * * 说明: 请通过以下方式来加载这个类库:$this->load->library('xml'); * */ class Xml { function Xml () { } private $document; private $filename; public function load($file) { /** * 载入需要解析的XML文件 * * $file:需解析的XML文件及其路径(请省略 .xml 扩展名,此处路径为相对于 APPPATH 的路径) * 例如:test.xml 文件保存在 application/config/test.xml 这个位置,那么 $file='config/test.xml' 。 * 请确保路径正确,否则无法解析。 */ $bad = array('|//+|', '|\.\./|'); $good = array('/', ''); $file = APPPATH.preg_replace ($bad, $good, $file);//生成需要解析的.xml文件的完整路径 if (! file_exists ($file)) { return false; //如果在指定位置找不到要解析的.xml文件,返回false } $this->document = file_get_contents($file); //将整个文件读入字符串 $this->filename = $file; return true; } /* load函数结束 */ public function parse() { /*** * @public * 对一个XML文档进行解析,结果存入数组 */ $xml = $this->document; if ($xml == '') { return false; //如果XML内容为空,则返回false } $doc = new DOMDocument (); $doc->preserveWhiteSpace = false; if ($doc->loadXML ($xml)) { $array = $this->flatten_node ($doc); if (count ($array) > 0) { return $array; } } return false; } /* parse函数结束 */ private function flatten_node($node) { /*** * @private(私有函数) * 将XML文档解析为一个数组 */ $array = array(); foreach ($node->childNodes as $child) { if ($child->hasChildNodes ()) { if ($node->firstChild->nodeName == $node->lastChild->nodeName && $node->childNodes->length > 1) { $array[$child->nodeName][] = $this->flatten_node ($child); } else { $array[$child->nodeName][] = $this->flatten_node($child); if ($child->hasAttributes ()) { $index = count($array[$child->nodeName])-1; $attrs =& $array[$child->nodeName][$index]['__attrs']; foreach ($child->attributes as $attribute) { $attrs[$attribute->name] = $attribute->value; } } } } else { return $child->nodeValue; } } return $array; } /* node_to_array函数结束 */ public function set($file,$node,$new_value) { /** * @public(公有函数) * 修改XML文档中某个节点的值 * 各个参数含义如下: $file为要修改的XML文件,$node为要修改其值的结点名称,$new_value为新值 * 如果修改成功,函数将返回true */ $bad = array('|//+|', '|\.\./|'); $good = array('/', ''); $file = APPPATH.preg_replace ($bad, $good, $file);//生成需要解析的.xml文件的完整路径 if (! file_exists ($file)) { return false; //如果在指定位置找不到要解析的.xml文件,返回false } else { $xml = new DOMDocument(); //实例化DOMDocument对象 $xml->load($file); //加载XML文件 if($xml->getElementsByTagName($node)!=null) { //如果XML文件中存在相应结点 foreach($xml->getElementsByTagName($node) as $list) { $list->nodeValue=$new_value;//将结点的值修改为参数中给出的新值 $xml->save($file);//保存修改之后的文件 return true; //返回true } } else { return false; //如果找不到参数中给出的结点,则返回false; } } } /* update函数结束 */ public function get($file,$node) { /** * @public(公有函数) * 获取XML文档中某个节点的值 * 各个参数含义如下: $file为要修改的XML文件,$node为要获取其值的结点名称 * 如果修改成功,函数将返回获取结果 */ $bad = array('|//+|', '|\.\./|'); $good = array('/', ''); $file = APPPATH.preg_replace ($bad, $good, $file);//生成需要解析的.xml文件的完整路径 if (! file_exists ($file)) { return false; //如果在指定位置找不到要解析的.xml文件,返回false } else { $xml = new DOMDocument(); //实例化DOMDocument对象 $xml->load($file); //加载XML文件 if($xml->getElementsByTagName($node)!=null) { //如果XML文件中存在相应结点 foreach($xml->getElementsByTagName($node) as $list) { return $list->nodeValue;//返回结果 } } else { return false; //如果找不到参数中给出的结点,则返回false; } } } } /* * XML.php 文件结束。此文件的正确保存路径应该是 application/libraries/XML.php * 请注意: 此处省略 ?> 闭合标签是为了避免出现错误,并非遗漏。 */ |
请将此文件保存在此路径下: application/libraries/XML.php ,然后通过
1 | $this->load->library('xml'); |
来加载这个扩展就可以了。
CodeIgniter | Tags: CodeIgniter, XML


