当前位置:Gxlcms > PHP教程 > PHP解析xml格式数据工具类示例

PHP解析xml格式数据工具类示例

时间:2021-07-01 10:21:17 帮助过:54人阅读

本文实例讲述了PHP解析xml格式数据工具类。分享给大家供大家参考,具体如下:

  1. class ome_xml {
  2. /**
  3. * xml资源
  4. *
  5. * @var resource
  6. * @see xml_parser_create()
  7. */
  8. public $parser;
  9. /**
  10. * 资源编码
  11. *
  12. * @var string
  13. */
  14. public $srcenc;
  15. /**
  16. * target encoding
  17. *
  18. * @var string
  19. */
  20. public $dstenc;
  21. /**
  22. * the original struct
  23. *
  24. * @access private
  25. * @var array
  26. */
  27. public $_struct = array();
  28. /**
  29. * Constructor
  30. *
  31. * @access public
  32. * @param mixed [$srcenc] source encoding
  33. * @param mixed [$dstenc] target encoding
  34. * @return void
  35. * @since
  36. */
  37. function SofeeXmlParser($srcenc = null, $dstenc = null) {
  38. $this->srcenc = $srcenc;
  39. $this->dstenc = $dstenc;
  40. // initialize the variable.
  41. $this->parser = null;
  42. $this->_struct = array();
  43. }
  44. /**
  45. * Parses the XML file
  46. *
  47. * @access public
  48. * @param string [$file] the XML file name
  49. * @return void
  50. * @since
  51. */
  52. function xml2array($file) {
  53. //$this->SofeeXmlParser('utf-8');
  54. $data = file_get_contents($file);
  55. $this->parseString($data);
  56. return $this->getTree();
  57. }
  58. function xml3array($file){
  59. $data = file_get_contents($file);
  60. $this->parseString($data);
  61. return $this->_struct;
  62. }
  63. /**
  64. * Parses a string.
  65. *
  66. * @access public
  67. * @param string data XML data
  68. * @return void
  69. */
  70. function parseString($data) {
  71. if ($this->srcenc === null) {
  72. $this->parser = xml_parser_create();
  73. } else {
  74. if($this->parser = xml_parser_create($this->srcenc)) {
  75. return 'Unable to create XML parser resource with '. $this->srcenc .' encoding.';
  76. }
  77. }
  78. if ($this->dstenc !== null) {
  79. @xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, $this->dstenc) or die('Invalid target encoding');
  80. }
  81. xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0); // lowercase tags
  82. xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1); // skip empty tags
  83. if (!xml_parse_into_struct($this->parser, $data, $this->_struct)) {
  84. /*printf("XML error: %s at line %d",
  85. xml_error_string(xml_get_error_code($this->parser)),
  86. xml_get_current_line_number($this->parser)
  87. );*/
  88. $this->free();
  89. return false;
  90. }
  91. $this->_count = count($this->_struct);
  92. $this->free();
  93. }
  94. /**
  95. * return the data struction
  96. *
  97. * @access public
  98. * @return array
  99. */
  100. function getTree() {
  101. $i = 0;
  102. $tree = array();
  103. $tree = $this->addNode(
  104. $tree,
  105. $this->_struct[$i]['tag'],
  106. (isset($this->_struct[$i]['value'])) ? $this->_struct[$i]['value'] : '',
  107. (isset($this->_struct[$i]['attributes'])) ? $this->_struct[$i]['attributes'] : '',
  108. $this->getChild($i)
  109. );
  110. unset($this->_struct);
  111. return $tree;
  112. }
  113. /**
  114. * recursion the children node data
  115. *
  116. * @access public
  117. * @param integer [$i] the last struct index
  118. * @return array
  119. */
  120. function getChild(&$i) {
  121. // contain node data
  122. $children = array();
  123. // loop
  124. while (++$i < $this->_count) {
  125. // node tag name
  126. $tagname = $this->_struct[$i]['tag'];
  127. $value = isset($this->_struct[$i]['value']) ? $this->_struct[$i]['value'] : '';
  128. $attributes = isset($this->_struct[$i]['attributes']) ? $this->_struct[$i]['attributes'] : '';
  129. switch ($this->_struct[$i]['type']) {
  130. case 'open':
  131. // node has more children
  132. $child = $this->getChild($i);
  133. // append the children data to the current node
  134. $children = $this->addNode($children, $tagname, $value, $attributes, $child);
  135. break;
  136. case 'complete':
  137. // at end of current branch
  138. $children = $this->addNode($children, $tagname, $value, $attributes);
  139. break;
  140. case 'cdata':
  141. // node has CDATA after one of it's children
  142. $children['value'] .= $value;
  143. break;
  144. case 'close':
  145. // end of node, return collected data
  146. return $children;
  147. break;
  148. }
  149. }
  150. //return $children;
  151. }
  152. /**
  153. * Appends some values to an array
  154. *
  155. * @access public
  156. * @param array [$target]
  157. * @param string [$key]
  158. * @param string [$value]
  159. * @param array [$attributes]
  160. * @param array [$inner] the children
  161. * @return void
  162. * @since
  163. */
  164. function addNode($target, $key, $value = '', $attributes = '', $child = '') {
  165. if (!isset($target[$key]['value']) && !isset($target[$key][0])) {
  166. if ($child != '') {
  167. $target[$key] = $child;
  168. }
  169. if ($attributes != '') {
  170. foreach ($attributes as $k => $v) {
  171. $target[$key][$k] = $v;
  172. }
  173. }
  174. $target[$key]['value'] = $value;
  175. } else {
  176. if (!isset($target[$key][0])) {
  177. // is string or other
  178. $oldvalue = $target[$key];
  179. $target[$key] = array();
  180. $target[$key][0] = $oldvalue;
  181. $index = 1;
  182. } else {
  183. // is array
  184. $index = count($target[$key]);
  185. }
  186. if ($child != '') {
  187. $target[$key][$index] = $child;
  188. }
  189. if ($attributes != '') {
  190. foreach ($attributes as $k => $v) {
  191. $target[$key][$index][$k] = $v;
  192. }
  193. }
  194. $target[$key][$index]['value'] = $value;
  195. }
  196. return $target;
  197. }
  198. /**
  199. * Free the resources
  200. *
  201. * @access public
  202. * @return void
  203. **/
  204. function free() {
  205. if (isset($this->parser) && is_resource($this->parser)) {
  206. xml_parser_free($this->parser);
  207. unset($this->parser);
  208. }
  209. }
  210. }

PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:

在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson

在线格式化XML/在线压缩XML
http://tools.jb51.net/code/xmlformat

XML在线压缩/格式化工具:
http://tools.jb51.net/code/xml_format_compress

XML代码在线格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP针对XML文件操作技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP错误与异常处理方法总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

人气教程排行