class_xml.php 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace DNSManager\opensrs;
  3. class class_xml {
  4. public function __construct () {}
  5. public function __destruct() {}
  6. public function xml2array($fileLocation){
  7. $xml_values = array();
  8. if (!file_exists($fileLocation)){
  9. trigger_error("oSRS Error - Unable to find config file, please make sure paths and active config file name are correct in openSRS_config.php.");
  10. $result = false;
  11. }
  12. else{
  13. $contents = file_get_contents($fileLocation);
  14. $parser = xml_parser_create('');
  15. if(!$parser) return false;
  16. xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
  17. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  18. xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  19. xml_parse_into_struct($parser, trim($contents), $xml_values);
  20. xml_parser_free($parser);
  21. if (!$xml_values) return array();
  22. $xml_array = array();
  23. $last_tag_ar =& $xml_array;
  24. $parents = array();
  25. $last_counter_in_tag = array(1=>0);
  26. foreach ($xml_values as $data) {
  27. switch($data['type']) {
  28. case 'open':
  29. $last_counter_in_tag[$data['level']+1] = 0;
  30. $new_tag = array('name' => $data['tag']);
  31. if(isset($data['attributes'])) $new_tag['attributes'] = $data['attributes'];
  32. if(isset($data['value']) && trim($data['value'])) $new_tag['value'] = trim($data['value']);
  33. $last_tag_ar[$last_counter_in_tag[$data['level']]] = $new_tag;
  34. $parents[$data['level']] =& $last_tag_ar;
  35. $last_tag_ar =& $last_tag_ar[$last_counter_in_tag[$data['level']]++];
  36. break;
  37. case 'complete':
  38. $new_tag = array('name' => $data['tag']);
  39. if(isset($data['attributes'])) $new_tag['attributes'] = $data['attributes'];
  40. if(isset($data['value']) && trim($data['value'])) $new_tag['value'] = trim($data['value']);
  41. $last_count = count($last_tag_ar)-1;
  42. $last_tag_ar[$last_counter_in_tag[$data['level']]++] = $new_tag;
  43. break;
  44. case 'close':
  45. $last_tag_ar =& $parents[$data['level']];
  46. break;
  47. default:
  48. break;
  49. };
  50. }
  51. $result = $xml_array;
  52. }
  53. return $result;
  54. }
  55. public function getValueByPath($xml_array, $tagPath) {
  56. $tmp_arr =& $xml_array;
  57. $tag_path = explode('/', $tagPath);
  58. foreach($tag_path as $tag_name) {
  59. $res = false;
  60. foreach($tmp_arr as $key => $node) {
  61. if(is_int($key) && $node['name'] == $tag_name) {
  62. $tmp_arr = $node;
  63. $res = true;
  64. break;
  65. }
  66. }
  67. if(!$res) return false;
  68. }
  69. return $tmp_arr;
  70. }
  71. public function renameFile ($path, $input) {
  72. $result = rename ($path . $input .".xml", $path . $input .".txt");
  73. return $result;
  74. }
  75. }
  76. // $arr = xml2array('test.xml');
  77. // print_r(getValueByPath($arr, 'tag/sub_tag/sub_sub_tag'));