Jump to content

XML to SQL in PHP


john6384

Recommended Posts

Hello, first of all I have some AJAX that POST's a PHP files output like so:

//XMLResults is a string of XML
...
        XMLResults += '/>';

var objHTTP, strResult;		

objHTTP=new XMLHttpRequest();

objHTTP.open('POST', "PutData.php", false);
objHTTP.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
objHTTP.overrideMimeType('text/html');

objHTTP.send("xml=" + xmlDoc);

strResult=objHTTP.responseText;

 

Now in PutData.php I need to convert from XML to SQL.

 

What is the best way of doing this?

 

I have this to get the data from the POST:

 

foreach($_POST as $key => $data){
mysql_query(SQLFromXML($data)) or die (mysql_error());
}

 

Then I will need a function to do the work - please describe the structure of this and give any hints or point me to code.

 

Thankyou

Link to comment
Share on other sites

Well this function was given to me but I dont think its complete and also it gives back in the alert in the AJAX - query was empty.

 


function SQLFromXML($doc){
global $sqlString;

//Initialize the XML parser
$parser=xml_parser_create();

//Function to use at the start of an element
function start($parser,$element_name,$element_attrs)
{
	global $sqlString;
	$sqlString="insert into " . $element_name . "(";
	$seperator="";

	foreach ($element_attrs as $key => $value) {
		$sqlString=$sqlString . $seperator . $key;				
		$seperator=",";
	}

	$sqlString=$sqlString . ") values(";
	$seperator="";

	foreach ($element_attrs as $key => $value) {
		$sqlString=$sqlString . $seperator . "'" . $value . "'";
		$seperator=",";
	}

	$sqlString=$sqlString . ")";
}

//Function to use at the end of an element
function stop($parser,$element_name)
{
//	echo "<br />";
}

//Function to use when finding character data
function char($parser,$data)
{
//	echo $data;
}

//Specify element handler
xml_set_element_handler($parser,"start","stop");

//Specify data handler
xml_set_character_data_handler($parser,"char");

//parse data
xml_parse($parser,$doc);

//Free the XML parser
xml_parser_free($parser);

return $sqlString;
}

 

Any comments on why this is returned in the alert?

Cheers

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.