forked from smeighan/nutcracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_xml_file.php
More file actions
97 lines (95 loc) · 2.34 KB
/
load_xml_file.php
File metadata and controls
97 lines (95 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
$arrData = array();
// if input is object, convert into array
if (is_object($arrObjData))
{
$arrObjData = get_object_vars($arrObjData);
}
if (is_array($arrObjData))
{
foreach ($arrObjData as $index => $value)
{
if (is_object($value) || is_array($value))
{
$value = objectsIntoArray($value, $arrSkipIndices); // recursive call
}
if (in_array($index, $arrSkipIndices))
{
continue;
}
$arrData[$index] = $value;
}
}
return $arrData;
}
// make sure we are only running this on a local machine
if($_SERVER['HTTP_HOST'] !='localhost')
{
echo"<pre><h1>ERROR! You cannot run this script on any place except for your local computer<br/>";
echo "<pre>Here are the values in your _SERVER array\n";
print_r($_SERVER);
echo "</pre>";
die ("\n\nProgram exiting .. ");
}
function load_xml_file($xmlUrl)
{
$dir=getcwd();
require_once($dir . '/conf/config.php');
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link)
{
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db)
{
die("Unable to select database");
}
//$xmlUrl = "seqbuilder5.xml"; // XML feed file/URL
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
$arrXml = objectsIntoArray($xmlObj);
echo "<pre>";
foreach($arrXml as $table => $arr)
{
echo "\n\nTable: $table\n";
$c=count($arr);
if($table <> 'comment')
{
for($i=0;$i<$c;$i++)
{
echo "$i ";
$cols = $arr[$i];
//print_r($cols);
$keys=array_keys($cols);
$values=array_values($cols);
$query = "replace into $table (";
$keyc = count($keys)-1;
foreach ($keys as $j=>$val)
{
$query .= sprintf ("%s",$val);
if($j<$keyc) $query .= sprintf(",");
}
$query .= sprintf (") values (");
foreach ($values as $j=>$val)
{
if(is_string($val))
$val_clean = addslashes($val);
else
$val_clean=$val;
$query .= sprintf ("'%s'",$val_clean);
if($j<$keyc) $query .= sprintf(",");
}
$query .= sprintf (")");
echo "$query\n";
$result=mysql_query($query) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $query . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());
}
}
}
echo "</pre>\n";
}
?>