Hi
Newbie question time.
I'm trying to parse some XML recieved via HTTP post from a form in the below format:
<?xml version="1.0" encoding="ISO-8859-1"?>
<job>
<command>add</command>
<username>bobsmith</username>
<password>p455w0rd</password>
<contact_name>Bob Smith</contact_name>
<contact_email>bob@smith.com</contact_email>
<contact_telephone>020 7987 6900</contact_telephone>
</job>
I'm OK parsing it if the above is saved as a file but not sure how to parse it via HTTP post:
<?php
require("MagicParser.php");
function myRecordHandler($record)
{
print $record["COMMAND"] . '<br>';
print $record["PASSWORD"] . '<br>';
// etc
}
$data = $_POST['DATA'];
MagicParser_parse(MagicParser_createFile($data),"myRecordHandler","xml|JOB/");
?>
Any help would be appreciated.
Regards
Darren
Thanks for the quick reply.
It now works provided there is no <?xml version="1.0" encoding="ISO-8859-1"?>
at the top of the XML to be parsed.
What do I need to do to handle this?
Regards
Darren
Hi Darren,
The standard XML declaration should be handled correctly. The problem may be white space surrounding the XML. To see if this is the case, try using the trim() function around the posted data. To do this, change:
$data = $_POST['DATA'];
to
$data = trim($_POST['DATA']);
Hope this helps,
Cheers,
David.
Hi David
Didn't work sadly.
I'm just pasting the above XML into a form and posting it to the script. I can't imagine there is any problem with the Form page but just in case:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>Test page</title>
</head>
<body>
<form method="post" action="testparser.php">
<b>stuff to parse:</b><br>
<textarea name="DATA" rows="15" cols="90"></textarea>
<input type="submit" name="submit" value="Send Stuff">
</form>
</body>
</html>
Regards
Darren
Hi Darren,
I've just recreated your code exactly, and it looks like it is PHP's "Magic Quotes" that is causing the problem. PHP is adding a backslash character to escape the " within the XML declaration, which is why it doesn't work when you use the declaration.
What you need to do is use the stripslashes() function, and I would recommend that you also stick with trim() to make sure your input is clean. This would make your code to load the $data variable as follows:
$data = trim(stripslashes($_POST['DATA']));
It should then work fine!
Cheers,
David.
It works!
Thanks for your assistance David.
Regards
Darren
Hi Darren,
Apologies for the confusion - MagicParser_createFile has been depreciated as it now has a built in string handler that will do what you require. All you need to do is prefix your POST data with "string://", something like this:
<?php
require("MagicParser.php");
function myRecordHandler($record)
{
print $record["COMMAND"] . '<br>';
print $record["PASSWORD"] . '<br>';
// etc
}
$data = $_POST['DATA'];
MagicParser_parse("string://".$data,"myRecordHandler","xml|JOB/");
?>
That should do the trick!
Cheers,
David.