Page 1 of 1

XML Issue

Posted: Tue May 26, 2009 4:46 am
by cusackd
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.

Code: Select all

 
 
<?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.

Code: Select all

 
<?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>
 
 

If anyone could help it would be great.

Re: XML Issue

Posted: Tue May 26, 2009 2:56 pm
by Weirdan
You're trying to load literal string '$post' instead of $post variable contents. Remove single quotes around the '$post' on this line:

Code: Select all

$dom->loadXml('$post'); // should be $dom->loadXml($post);

Re: XML Issue

Posted: Wed May 27, 2009 4:25 am
by cusackd
Still not working below are the errors

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