Inserindo no Mysql do XML

Abril 10, 2007 on 6:47 pm | In Informatica |

Essa é uma classe pronta para criar insert apartir de um XML, um amigo meu me enviou e estou compartilhando com vocês, falta de tempo para codar é assim, os amigos compartilham entre si... : P Dúvidas? E-mail para o dono e coments no POST.

Boa diversão

CODE:
  1. /***
  2. * Class retirada do http://php.net
  3. * Duvida : mzpiva@gmail.com
  4. */
  5. class xml2Array {
  6.  
  7. var $arrOutput = array();
  8. var $resParser;
  9. var $strXmlData;
  10.  
  11. function parse($strInputXML) {
  12.  
  13. $this->resParser = xml_parser_create ();
  14. xml_set_object($this->resParser,$this);
  15. xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
  16.  
  17. xml_set_character_data_handler($this->resParser, "tagData");
  18.  
  19. $this->strXmlData = xml_parse($this->resParser,$strInputXML );
  20. if(!$this->strXmlData) {
  21. die(sprintf("XML error: %s at line %d",
  22. xml_error_string(xml_get_error_code($this->resParser)),
  23. xml_get_current_line_number($this->resParser)));
  24. }
  25.  
  26. xml_parser_free($this->resParser);
  27.  
  28. return $this->arrOutput;
  29. }
  30. function tagOpen($parser, $name, $attrs) {
  31. $tag=array("name"=>$name,"attrs"=>$attrs);
  32. array_push($this->arrOutput,$tag);
  33. }
  34.  
  35. function tagData($parser, $tagData) {
  36. if(trim($tagData)) {
  37. if(isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) {
  38. $this->arrOutput[count($this->arrOutput)-1]['tagData'] .= $tagData;
  39. }
  40. else {
  41. $this->arrOutput[count($this->arrOutput)-1]['tagData'] = $tagData;
  42. }
  43. }
  44. }
  45.  
  46. function tagClosed($parser, $name) {
  47. $this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1];
  48. array_pop($this->arrOutput);
  49. }
  50. }
  51.  
  52. /***
  53. * Abre arquivo XML, sem limite de leitura, adicionando cada linha em uma posição de array.
  54. */
  55. $file = file('XML/'.$f_name);
  56.  
  57. /***
  58. * Converte array em linhas. XML Parse
  59. */
  60. foreach ($file as $line) {
  61. $xmlParse .= $line;
  62. }
  63.  
  64. /***
  65. * Tranforma XML em Array
  66. */
  67. $class = new xml2Array();
  68. $arrayXml = $class->parse($xmlParse);
  69.  
  70. /***
  71. * Captura Campos e Valores para query de Insert e adiciona ao array.
  72. */
  73. foreach ($arrayXml[0] as $arrayChildren) {
  74. if (is_array($arrayChildren)) {
  75. $s = 0;
  76. foreach ($arrayChildren as $values) {
  77. $i = 0;
  78. foreach ($values['children'] as $children) {
  79. $sql[$s][$i]['i'] = $children['name'];      // Campos
  80. $sql[$s][$i]['v'] = $children['tagData'];   // Valores
  81. $i++;
  82. }
  83. $s++;
  84. }
  85. }
  86. }
  87.  
  88. /***
  89. * Monta Querys para Insert.
  90. */
  91. foreach ($sql as $record) {
  92.  
  93. // Campos
  94. $i = 0;
  95. foreach ($record as $value) {
  96. $arrayInsert[$i] = $value['i'];
  97. $i++;
  98. }
  99.  
  100. // Valores
  101. $i = 0;
  102. foreach ($record as $value) {
  103. $arrayValues[$i] = $value['v'];
  104. $i++;
  105. }
  106.  
  107. // Exibe Querys
  108. $query= 'INSERT INTO tabela_x (' . implode(', ', $arrayInsert) . ') VALUES (\'' . implode('\', \'', $arrayValues)'\')';
  109. $res = mysql_query($query) or die ("Error in query: $query. " .mysql_error());
  110.  
  111. //    print "\r\n";
  112. }


No Comments yet »

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>


 

 Assine o feed