XML Issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cusackd
Forum Newbie
Posts: 5
Joined: Fri Apr 18, 2008 4:01 am

XML Issue

Post 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.
Last edited by Benjamin on Tue May 26, 2009 10:58 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: XML Issue

Post 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);
cusackd
Forum Newbie
Posts: 5
Joined: Fri Apr 18, 2008 4:01 am

Re: XML Issue

Post 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
Post Reply