cusackd Posted May 26, 2009 Share Posted May 26, 2009 Hi Lads trying to parse data from xml in to a mysql database i will show you my code so you can get an idea of whats going on, the xml data will be posted to my script. <?php // XML Gets Posted automatically by text messaging servuce $post= $_POST['xml']; $dom = new domDocument; $dom->loadXML('$post'); if (!$dom) { echo 'Error while parsing the document'; exit; } $sitemap = simplexml_import_dom($dom); echo $sitemap->originator; echo "<br>"; echo $sitemap->recipient; echo "<br>"; echo $sitemap->date; echo "<br>"; echo $sitemap->message; echo "<br>"; $originator = $sitemap->originator; $recipient = $sitemap->recipient; $date = $sitemap->date; $message = $sitemap->message; echo $originator; echo $recipient; echo $date; echo $message; $con = mysql_connect("db","usr","pw"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("anua200_textbookings", $con); $originator = mysql_real_escape_string($originator); $recipient = mysql_real_escape_string($recipient); $date = mysql_real_escape_string($date); $message = mysql_real_escape_string($message); $sql="INSERT INTO text_bookings ( originator, recipient, date, message ) VALUES ( '$originator', '$recipient', '$date', '$message' )"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } else { echo "Succesfully added data"; } mysql_close($con); ?> The Error Message when using this is as follows. Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: Start tag expected, '<' not found in Entity, line: 1 in D:\Domains\cusack.webworld.ie\wwwroot\process2.php on line 5 Warning: simplexml_import_dom() [function.simplexml-import-dom]: Invalid Nodetype to import in D:\Domains\cusack.webworld.ie\wwwroot\process2.php on line 34 The XML data that needs to be parsed. <?xml version="1.0" ?> <sms> <originator>+353872536719</originator> <recipient>+353857547357</recipient> <date>05/21/2009 12:35:38 PM</date> <message><![CDATA[Yes%20will%20be%20there%20thanks]]></message> </sms> Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/ Share on other sites More sharing options...
JonnoTheDev Posted May 26, 2009 Share Posted May 26, 2009 Bad Syntax $dom->loadXML('$post'); Corrected $dom->loadXML($post); Variables enclosed in single quotes are treated as a string. Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-842404 Share on other sites More sharing options...
cusackd Posted May 26, 2009 Author Share Posted May 26, 2009 Bad Syntax $dom->loadXML('$post'); Corrected $dom->loadXML($post); Variables enclosed in single quotes are treated as a string. Now we have a new error after correction. Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: Empty string supplied as input in D:\Domains\cusack.webworld.ie\wwwroot\process2.php on line 5 Warning: simplexml_import_dom() [function.simplexml-import-dom]: Invalid Nodetype to import in D:\Domains\cusack.webworld.ie\wwwroot\process2.php on line 34 The following is line 5 and 34 Line 5 $dom->loadXML($post); Line 34 $sitemap = simplexml_import_dom($dom); Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-842419 Share on other sites More sharing options...
JonnoTheDev Posted May 26, 2009 Share Posted May 26, 2009 This array value must not contain any data: $_POST['xml']; Check your form is correct. Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-842424 Share on other sites More sharing options...
cusackd Posted May 26, 2009 Author Share Posted May 26, 2009 This array value must not contain any data: $_POST['xml']; Check your form is correct. The following is the HTML Post form <form action='http://cusack.webworld.ie/process2.php' method='POST'> <!--<form action='process.php' method='POST'>--> <table border='0' align='center'> <textarea name='XML'></textarea> <br> <input type='submit' value='Submit'> </table> </form> Just changed it to the following <form action='http://cusack.webworld.ie/process2.php' method='POST'> <!--<form action='process.php' method='POST'>--> <table border='0' align='center'> <!--Lower case textarea name--!> <textarea name='xml'></textarea> <br> <input type='submit' value='Submit'> </table> </form> Now i am recieving the following Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: String not started expecting ' or " in Entity, line: 1 in D:\Domains\cusack.webworld.ie\wwwroot\process2.php on line 5 Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: Malformed declaration expecting version in Entity, line: 1 in D:\Domains\cusack.webworld.ie\wwwroot\process2.php on line 5 Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: Blank needed here in Entity, line: 1 in D:\Domains\cusack.webworld.ie\wwwroot\process2.php on line 5 Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: parsing XML declaration: '?>' expected in Entity, line: 1 in D:\Domains\cusack.webworld.ie\wwwroot\process2.php on line 5 Warning: simplexml_import_dom() [function.simplexml-import-dom]: Invalid Nodetype to import in D:\Domains\cusack.webworld.ie\wwwroot\process2.php on line 34 Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-842433 Share on other sites More sharing options...
cusackd Posted May 27, 2009 Author Share Posted May 27, 2009 Bump as need to get this sorted. Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-842958 Share on other sites More sharing options...
JonnoTheDev Posted May 27, 2009 Share Posted May 27, 2009 Why have you not written a test program prior to using a form to check that your XML is valid? Start with a fixed variable holding the XML and then debug from there. Your XML is probably bad. Simple debugging. Have you read the DOMDocument class documentation in the php manual? http://uk2.php.net/manual/en/class.domdocument.php Here is a start: <?php // xml to be loaded $xml = "<root><node/></root>"; if(!$document = @DOMDocument::loadXML($xml)) { print "The XML is not valid."; } else { print "XML valid."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-842959 Share on other sites More sharing options...
cusackd Posted May 27, 2009 Author Share Posted May 27, 2009 When holding a fixed variable the data goes through fine with no problems at all thats why i am baffled that it doesnt go through when it is sent to the script. Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-842979 Share on other sites More sharing options...
JonnoTheDev Posted May 27, 2009 Share Posted May 27, 2009 print the post data to the screen. has it changed? do you have magic quotes enabled that are adding slashes? Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-842984 Share on other sites More sharing options...
cusackd Posted May 27, 2009 Author Share Posted May 27, 2009 print the post data to the screen. has it changed? do you have magic quotes enabled that are adding slashes? When printing the data to the screen it seems like it changes considerably +353871234567 +353877654321 03/31/2009 12:59:30 PM Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: String not started expecting ' or " in Entity, line: 1 in D:\Domains\cusack.webworld.ie\wwwroot\process2.php on line 8 Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: Malformed declaration expecting version in Entity, line: 1 in D:\Domains\cusack.webworld.ie\wwwroot\process2.php on line 8 Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: Blank needed here in Entity, line: 1 in D:\Domains\cusack.webworld.ie\wwwroot\process2.php on line 8 Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: parsing XML declaration: '?>' expected in Entity, line: 1 in D:\Domains\cusack.webworld.ie\wwwroot\process2.php on line 8 Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: Input is not proper UTF-8, indicate encoding ! Bytes: 0x92 0x73 0x20 0x67 in Entity, line: 11 in D:\Domains\cusack.webworld.ie\wwwroot\process2.php on line 8 Warning: simplexml_import_dom() [function.simplexml-import-dom]: Invalid Nodetype to import in D:\Domains\cusack.webworld.ie\wwwroot\process2.php on line 37 Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-842991 Share on other sites More sharing options...
aschk Posted May 27, 2009 Share Posted May 27, 2009 Do you want to show us what it changed to, or would you like us to guess? Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-842993 Share on other sites More sharing options...
JonnoTheDev Posted May 27, 2009 Share Posted May 27, 2009 print it between XMP tags as it will be parsed as HTML. Also exit the script after you have printed. print "<xmp>"; print $_POST['xml']; print "</xmp>"; exit(); Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-842995 Share on other sites More sharing options...
cusackd Posted May 27, 2009 Author Share Posted May 27, 2009 print it between XMP tags as it will be parsed as HTML. Also exit the script after you have printed. print "<xmp>"; print $_POST['xml']; print "</xmp>"; exit(); Now this is whats being displyed <?xml version=\"1.0\" ?> <sms> <originator>+353871234567</originator> <recipient>+353877654321</recipient> <date>03/31/2009 12:59:30 PM</date> <message><![CDATA[That’s great! Many thanks for getting back to me]]></message> </sms> Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-843002 Share on other sites More sharing options...
cusackd Posted May 27, 2009 Author Share Posted May 27, 2009 Do you want to show us what it changed to, or would you like us to guess? No need to be smart im learning here mate. Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-843004 Share on other sites More sharing options...
thebadbad Posted May 27, 2009 Share Posted May 27, 2009 Automatic escaping of quotes is turned on (the magic_quotes_gpc setting). Turn it off (recommended if you know how to handle user input) or run the data through stripslashes(). Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-843007 Share on other sites More sharing options...
aschk Posted May 27, 2009 Share Posted May 27, 2009 thebadbad beat me to it.... dang! Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-843010 Share on other sites More sharing options...
aschk Posted May 27, 2009 Share Posted May 27, 2009 magic_quotes_gpc is default turned off now as of php version 5.2.0 I think? You should be able to alter these settings at runtime instead of having to rely on stripslashes being inserted where it's needed. See below: ini_set('magic_quotes_gpc', '0'); Make sure this is at the top of any script you run (or in your config.php file is you have one). Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-843012 Share on other sites More sharing options...
thebadbad Posted May 27, 2009 Share Posted May 27, 2009 Or put this in a .htaccess file in your web root directory (if you haven't got access to the server's php.ini): php_flag magic_quotes_gpc off Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-843031 Share on other sites More sharing options...
cusackd Posted May 27, 2009 Author Share Posted May 27, 2009 Automatic escaping of quotes is turned on (the magic_quotes_gpc setting). Turn it off (recommended if you know how to handle user input) or run the data through stripslashes(). Ok now i have a new problem <?php $post= $_POST['xml']; $post = stripslashes($post); $post = "<xmp>$post</xmp>"; echo $post; $dom = new domDocument; $dom->loadXML($post); if (!$dom) { echo 'Error while parsing the document'; exit; } $sitemap = simplexml_import_dom($dom); echo $sitemap->originator; echo "<br>"; echo $sitemap->recipient; echo "<br>"; echo $sitemap->date; echo "<br>"; echo $sitemap->message; echo "<br>"; $originator = $sitemap->originator; $recipient = $sitemap->recipient; $date = $sitemap->date; $message = $sitemap->message; echo $originator; echo $recipient; echo $date; echo $message; $con = mysql_connect("123","456","789"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("anua200_textbookings", $con); $originator = mysql_real_escape_string($originator); $recipient = mysql_real_escape_string($recipient); $date = mysql_real_escape_string($date); $message = mysql_real_escape_string($message); $sql="INSERT INTO text_bookings ( originator, recipient, date, message ) VALUES ( '$originator', '$recipient', '$date', '$message' )"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } else { echo "Succesfully added data"; } mysql_close($con); ?> Error Report Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: XML declaration allowed only at the start of the document in Entity, line: 1 in D:\Domains\cusack.webworld.ie\wwwroot\process2.php on line 10 Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: Input is not proper UTF-8, indicate encoding ! Bytes: 0x92 0x73 0x20 0x67 in Entity, line: 11 in D:\Domains\cusack.webworld.ie\wwwroot\process2.php on line 10 Warning: simplexml_import_dom() [function.simplexml-import-dom]: Invalid Nodetype to import in D:\Domains\cusack.webworld.ie\wwwroot\process2.php on line 39 Cheers for the help lads but unfortunatly its taken its time to get sorted. Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-843067 Share on other sites More sharing options...
JonnoTheDev Posted May 27, 2009 Share Posted May 27, 2009 Get rid of this now you have finished debugging $post = "<xmp>$post</xmp>"; echo $post; Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-843085 Share on other sites More sharing options...
cusackd Posted May 27, 2009 Author Share Posted May 27, 2009 Get rid of this now you have finished debugging $post = "<xmp>$post</xmp>"; echo $post; That seems to have solved one error still getting the following Warning: DOMDocument::loadXML() [function.DOMDocument-loadXML]: Input is not proper UTF-8, indicate encoding ! Bytes: 0x92 0x73 0x20 0x67 in Entity, line: 11 in D:\Domains\cusack.webworld.ie\wwwroot\process2.php on line 10 Cheers for the help Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-843137 Share on other sites More sharing options...
cusackd Posted May 27, 2009 Author Share Posted May 27, 2009 Ok i used $post = utf8_encode($post); That stopped the error but no data is going in to the database. Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-843151 Share on other sites More sharing options...
cusackd Posted May 27, 2009 Author Share Posted May 27, 2009 ok in my effort to get it working i commented the following $sitemap = simplexml_import_dom($dom); I uncommented this and now for the final code <?php $post= $_POST['xml']; $post = stripslashes($post); $post = trim($post); $post = utf8_encode($post); echo $post; $dom = new domDocument; $dom->loadXML($post); if (!$dom) { echo 'Error while parsing the document'; exit; } $sitemap = simplexml_import_dom($dom); echo $sitemap->originator; echo "<br>"; echo $sitemap->recipient; echo "<br>"; echo $sitemap->date; echo "<br>"; echo $sitemap->message; echo "<br>"; $originator = $sitemap->originator; $recipient = $sitemap->recipient; $date = $sitemap->date; $message = $sitemap->message; echo $originator; echo $recipient; echo $date; echo $message; $con = mysql_connect("1234","5678","9"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("anua200_textbookings", $con); $originator = mysql_real_escape_string($originator); $recipient = mysql_real_escape_string($recipient); $date = mysql_real_escape_string($date); $message = mysql_real_escape_string($message); $sql="INSERT INTO text_bookings ( originator, recipient, date, message ) VALUES ( '$originator', '$recipient', '$date', '$message' )"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } else { echo "Succesfully added data"; } mysql_close($con); ?> Cheers for all the help lads you got me out of a serious hole. Much appriciated. Quote Link to comment https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-843167 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.