Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $xmlfile = new SimpleXMLElement($xml);
- xmlRecurse($xmlfile,0);
- function xmlRecurse($xmlObj,$depth=0,$xpath=null) {
- if (!isset($xpath))//if xpath is null
- {
- $xpath='/'.$xmlObj->getName().'/';//root element
- echo $xpath."n";
- db_store($xpath,$xpath,'nill',0);
- }
- $position = array();
- foreach($xmlObj->children() as $child) // if root has children
- {
- $name = $child->getName(); //get subchild
- //echo $name."n";
- if(isset($position[$name])) //position name is set to any value
- {
- ++$position[$name];
- }
- else {
- $position[$name]=1; //intial value of position for first root child
- }
- $path=$xpath.$name.'['.$position[$name].']';
- // echo $xpath.$name."n";
- echo str_repeat('-',$depth).">".$name.": $pathn";
- echo $name."n";
- db_store($name,$path,'er',$position[$name]);
- xmlRecurse($child,$depth+1,$path.'/');
- }
- }
- function db_store($name,$path,$parent,$position)
- {
- $mysql_hostname = "localhost"; // Example : localhost
- $mysql_user = "root";
- $mysql_password = "";
- $mysql_database = "xmlloaddata";
- $dbh = new PDO("mysql:dbname=
- {$mysql_database};host{$mysql_hostname};port=3306",
- $mysql_user, $mysql_password);
- $name=str_replace('/','',$name);
- $sql = $dbh->prepare("INSERT INTO `xmlload`
- (`node`,path,parent,ref_id) VALUES (?,?,?,?)");
- $sql->execute(array($name,$path,$parent,$position));
- }
- ?>
- items
- item
- url
- color
- item
- url
- color
- items
- item
- url
- color
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement