Advertisement
Guest User

Untitled

a guest
Mar 11th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. $xmlfile = new SimpleXMLElement($xml);
  2. xmlRecurse($xmlfile,0);
  3. function xmlRecurse($xmlObj,$depth=0,$xpath=null) {
  4. if (!isset($xpath))//if xpath is null
  5. {
  6. $xpath='/'.$xmlObj->getName().'/';//root element
  7. echo $xpath."n";
  8. db_store($xpath,$xpath,'nill',0);
  9. }
  10. $position = array();
  11. foreach($xmlObj->children() as $child) // if root has children
  12. {
  13. $name = $child->getName(); //get subchild
  14. //echo $name."n";
  15. if(isset($position[$name])) //position name is set to any value
  16. {
  17. ++$position[$name];
  18. }
  19. else {
  20. $position[$name]=1; //intial value of position for first root child
  21. }
  22. $path=$xpath.$name.'['.$position[$name].']';
  23. // echo $xpath.$name."n";
  24.  
  25. echo str_repeat('-',$depth).">".$name.": $pathn";
  26. echo $name."n";
  27. db_store($name,$path,'er',$position[$name]);
  28. xmlRecurse($child,$depth+1,$path.'/');
  29. }
  30. }
  31.  
  32. function db_store($name,$path,$parent,$position)
  33. {
  34. $mysql_hostname = "localhost"; // Example : localhost
  35. $mysql_user = "root";
  36. $mysql_password = "";
  37. $mysql_database = "xmlloaddata";
  38. $dbh = new PDO("mysql:dbname=
  39. {$mysql_database};host{$mysql_hostname};port=3306",
  40. $mysql_user, $mysql_password);
  41. $name=str_replace('/','',$name);
  42. $sql = $dbh->prepare("INSERT INTO `xmlload`
  43. (`node`,path,parent,ref_id) VALUES (?,?,?,?)");
  44. $sql->execute(array($name,$path,$parent,$position));
  45. }
  46. ?>
  47.  
  48. items
  49. item
  50. url
  51. color
  52. item
  53. url
  54. color
  55.  
  56. items
  57. item
  58. url
  59. color
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement