PHP封装的XML简单操作类完整实例

这篇文章主要介绍了PHP封装的XML简单操作类,结合完整实例形式分析了php针对xml文件进行载入、读取及写入相关操作技巧的封装与使用方法,需要的朋友可以参考下

本文实例讲述了PHP封装的XML简单操作类,分享给大家供大家参考,具体如下:

xml_dom.php封装类文件:

  1. <?php
  2. /**
  3. * Class xml_dom
  4. *
  5. nodeType:
  6. 1 XML_ELEMENT_NODE(元素类型)
  7. 2 XML_ATTRIBUTE_NODE
  8. 3 XML_TEXT_NODE
  9. 4 XML_CDATA_SECTION_NODE
  10. 5 XML_ENTITY_REFERENCE_NODE
  11. 6 XML_ENTITY_NODE
  12. 7 XML_PROCESSING_INSTRUCTION_NODE
  13. 8 XML_COMMENT_NODE(注释类型)
  14. 9 XML_DOCUMENT_NODE
  15. 10 XML_DOCUMENT_TYPE_NODE
  16. 11 XML_DOCUMENT_FRAGMENT_NODE
  17. 12 XML_NOTATION_NODE
  18. *
  19. PHP DOMDocument操作:
  20. 属性:
  21. Attributes 存储节点的属性列表(只读)
  22. childNodes 存储节点的子节点列表(只读)
  23. dataType 返回此节点的数据类型
  24. Definition 以DTD或XML模式给出的节点的定义(只读)
  25. Doctype 指定文档类型节点(只读)
  26. documentElement 返回文档的根元素(可读写)
  27. firstChild 返回当前节点的第一个子节点(只读)
  28. Implementation 返回XMLDOMImplementation对象
  29. lastChild 返回当前节点最后一个子节点(只读)
  30. nextSibling 返回当前节点的下一个兄弟节点(只读)
  31. nodeName 返回节点的名字(只读)
  32. nodeType 返回节点的类型(只读)
  33. nodeTypedValue 存储节点值(可读写)
  34. nodeValue 返回节点的文本(可读写)
  35. ownerDocument 返回包含此节点的根文档(只读)
  36. parentNode 返回父节点(只读)
  37. Parsed 返回此节点及其子节点是否已经被解析(只读)
  38. Prefix 返回名称空间前缀(只读)
  39. preserveWhiteSpace 指定是否保留空白(可读写)
  40. previousSibling 返回此节点的前一个兄弟节点(只读)
  41. Text 返回此节点及其后代的文本内容(可读写)
  42. url 返回最近载入的XML文档的URL(只读)
  43. Xml 返回节点及其后代的XML表示(只读)
  44. 方法:
  45. appendChild 为当前节点添加一个新的子节点,放在最后的子节点后
  46. cloneNode 返回当前节点的拷贝
  47. createAttribute 创建新的属性
  48. createCDATASection 创建包括给定数据的CDATA段
  49. createComment 创建一个注释节点
  50. createDocumentFragment 创建DocumentFragment对象
  51. createElement 创建一个元素节点
  52. createEntityReference 创建EntityReference对象
  53. createNode 创建给定类型,名字和命名空间的节点
  54. createPorcessingInstruction 创建操作指令节点
  55. createTextNode 创建包括给定数据的文本节点
  56. getElementsByTagName 返回指定名字的元素集合
  57. hasChildNodes 返回当前节点是否有子节点
  58. insertBefore 在指定节点前插入子节点
  59. Load 导入指定位置的XML文档
  60. loadXML 导入指定字符串的XML文档
  61. removeChild 从子结点列表中删除指定的子节点
  62. replaceChild 从子节点列表中替换指定的子节点
  63. Save 把XML文件存到指定节点
  64. selectNodes 对节点进行指定的匹配,并返回匹配节点列表
  65. selectSingleNode 对节点进行指定的匹配,并返回第一个匹配节点
  66. transformNode 使用指定的样式表对节点及其后代进行转换
  67. *
  68. */
  69. class xml_dom
  70. {
  71. protected $dblink; // xml连接
  72. protected $dbfile; // xml文件路径
  73. /**
  74. * xml文件 构造类
  75. * @param $db_file xml文件
  76. */
  77. public function __construct($db_file)
  78. {
  79. $this->dbfile = $db_file;
  80. if(!file_exists($db_file))
  81. {
  82. // die('未找到数据库文件');
  83. $this->dblink = new DOMDocument('1.0', 'utf-8');
  84. $root = $this->dblink->createElement('root');
  85. $this->dblink->appendChild($root);
  86. $this->dblink->formatOutput = true; // xml文件保留缩进样式
  87. $this->dblink->save($this->dbfile);
  88. }
  89. else
  90. {
  91. $this->dblink = new DOMDocument();
  92. $this->dblink->formatOutput = true;
  93. $this->dblink->load($this->dbfile);
  94. }
  95. }
  96. /**
  97. * 遍历所有元素
  98. * ===============================================
  99. * 标准xml文件,一个元素可能有n个属性,可用自定义键[nodevalue]获取元素值
  100. * <?xml version="1.0" encoding="utf-8"?>
  101. * <table name="posts">
  102. * <column name="id">1</column>
  103. * <column name="title">标题一</column>
  104. * <column name="content">详细内容一</column>
  105. * </table>
  106. * ===============================================
  107. * 简单xml文件,没有属性,键值一一对应
  108. * <?xml version="1.0" encoding="utf-8"?>
  109. * <root>
  110. * <posts>
  111. * <id>1</id>
  112. * <title>标题一</title>
  113. * <content>详细内容一</content>
  114. * </posts>
  115. * </root>
  116. * @param $node
  117. * @return array
  118. */
  119. function getData($node=0){
  120. if(!$node)
  121. {
  122. $node = $this->dblink->documentElement;
  123. }
  124. $array = array();
  125. foreach($node->attributes as $attribute)
  126. {
  127. $key = $attribute->nodeName;
  128. $val = $attribute->nodeValue;
  129. $array[$key] = $val;
  130. }
  131. if(count($array)) // 有属性,则用[nodevalue]键代表值
  132. {
  133. $array['nodevalue'] = $node->nodeValue;
  134. }
  135. // 递归遍历所有子元素
  136. $node_child = $node->firstChild;
  137. while($node_child)
  138. {
  139. if(XML_ELEMENT_NODE == $node_child->nodeType)
  140. {
  141. $tagname = $node_child->tagName;
  142. $result = $this->getData($node_child);
  143. if(isset($array[$tagname])) // 发现有重复tagName的子元素存在,所以改用数组存储重复tagName的所有子元素
  144. {
  145. if(!is_array($array[$tagname][0]))
  146. {
  147. $tmp = $array[$tagname];
  148. $array[$tagname] = array();
  149. $array[$tagname][] = $tmp;
  150. }
  151. $array[$tagname][] = $result;
  152. }
  153. else
  154. {
  155. $array[$tagname] = $result;
  156. }
  157. }
  158. $node_child = $node_child->nextSibling;
  159. }
  160. if(!count($array)) // 没有子元素&没有属性=最末子元素,就返回该元素的nodeValue值
  161. {
  162. return $node->nodeValue;
  163. }
  164. return $array;
  165. }
  166. /**
  167. * 把array数据写到xml文件(覆盖)
  168. * @param $data
  169. */
  170. public function setData($data,&$node=0)
  171. {
  172. $is_root = false;
  173. if(!$node)
  174. {
  175. $is_root = true;
  176. $node = $this->dblink->documentElement;
  177. // 清除原数据
  178. $remove = array();
  179. $node_child = $node->firstChild;
  180. while($node_child)
  181. {
  182. $remove[] = $node_child;
  183. $node_child = $node_child->nextSibling;
  184. }
  185. foreach($remove as $r)
  186. {
  187. $node->removeChild($r);
  188. }
  189. }
  190. if(is_array($data))
  191. {
  192. foreach($data as $k=>$v)
  193. {
  194. if(is_array($v))
  195. {
  196. foreach($v as $r)
  197. {
  198. $item = $this->dblink->createElement($k);
  199. $result = $this->setData($r,$item);
  200. $node->appendChild($result);
  201. }
  202. }
  203. else
  204. {
  205. $item = $this->dblink->createElement($k);
  206. $value = $this->dblink->createTextNode($v);
  207. $item->appendChild($value);
  208. $node->appendChild($item);
  209. }
  210. }
  211. }
  212. else
  213. {
  214. $item = $this->dblink->createTextNode($data);
  215. $node->appendChild($item);
  216. }
  217. if($is_root)
  218. {
  219. $this->dblink->save($this->dbfile); // 覆盖写入
  220. }
  221. else
  222. {
  223. return $node;
  224. }
  225. }
  226. }

简单用法示例如下:

smp.xml文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <root>
  3. <posts>
  4. <id>1</id>
  5. <title>标题一</title>
  6. <content>详细内容一</content>
  7. </posts>
  8. <posts>
  9. <id>2</id>
  10. <title>标题二</title>
  11. <content>详细内容二</content>
  12. </posts>
  13. <posts>
  14. <id>3</id>
  15. <title>标题三</title>
  16. <content>详细内容三</content>
  17. </posts>
  18. </root>

index.php文件:

  1. include("xml_dom.php");
  2. $xml=new xml_dom("smp.xml");//载入xml文件
  3. $xmlarr=$xml->getData();//读取xml文件内容
  4. var_dump($xmlarr);

运行结果:

  1. array(1) {
  2. ["posts"]=>
  3. array(3) {
  4. [0]=>
  5. array(3) {
  6. ["id"]=>
  7. string(1) "1"
  8. ["title"]=>
  9. string(9) "标题一"
  10. ["content"]=>
  11. string(15) "详细内容一"
  12. }
  13. [1]=>
  14. array(3) {
  15. ["id"]=>
  16. string(1) "2"
  17. ["title"]=>
  18. string(9) "标题二"
  19. ["content"]=>
  20. string(15) "详细内容二"
  21. }
  22. [2]=>
  23. array(3) {
  24. ["id"]=>
  25. string(1) "3"
  26. ["title"]=>
  27. string(9) "标题三"
  28. ["content"]=>
  29. string(15) "详细内容三"
  30. }
  31. }
  32. }