Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/
Share on other sites

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);
Link to comment
https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-842419
Share on other sites

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

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-842433
Share on other sites

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.";
}
?> 

Link to comment
https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-842959
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-842991
Share on other sites

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> 

 

 

Link to comment
https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-843002
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-843012
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-843067
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-843137
Share on other sites

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.  ;D

Link to comment
https://forums.phpfreaks.com/topic/159713-solved-xml-parsing/#findComment-843167
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.