Having a hard time parsing XML created from a web-form using POST.
Here’s the scenario:
1) User comes to a web-page, enters their name into a text-field, and clicks SUBMIT
2) This calls a PHP file (called “makeXML.php”) which generates an XML file containing that user’s name in a tag called “currentUserName”
3) an iPhone App then loads this “makeXML.php” file (using ‘loadXMLByURL’) and parses it, looking specifically to output the contents of the “currentUserName” tag into a UILabel object.
Should be pretty simple – but for some reason, the contents of the “currentUserName” tag are coming up empty in the App – though they show up perfectly well in the generated XML code in the browser.
What’s even stranger, is that if I instead hard-code a value to “uName” in the PHP file (“makeXML.php”) – as opposed to getting that value from the FORM (using $_POST[“userName”];) – it all works perfectly well. I’m able to grab the value from the “currentUserName” tag and output it to the UILabel object.
NSXMLParser seems to just not like POST’ed values for some reason.
Any ideas?
Here’s the code:
portal.html:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Web-Form</title>
</head>
<body>
<form name="form1" method="post" action="makeXML.php">
<p>Enter your name:</p>
<p>
<input name="userName" type="text" />
</p>
<p>
<input type="submit" value="SUBMIT" />
</p>
</form>
</body>
</html>
Here is “makeXML.php”:
<?php
header("Content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
echo "<document>";
$uName = $_POST["userName"];
echo "Here is the name you typed:";
echo "<br/>";
echo "<theUsersName>$uName</theUsersName>";
echo "</document>";
?>
Here is the outputted code from makeXML.php: (Note that this code comes out all on one line – is that how its supposed to be? Shouldn’t the “br” tag be working and forcing a line break there?)
<?xml version="1.0" encoding="UTF-8" ?><document>Here is the name you typed:<br/><theUsersName>johnson</theUsersName></document>
Again, note that if I replace:
$uName = $_POST["userName"];
with plain-old:
$uName = "John";
Everything works perfectly and the name “John” appears correctly in my UILabel…
As
psoftalluded to in his comments, it is hard to concretely answer a question such as this since your end goal seems somewhat unclear. I will assume that you are just starting to experiment with all of the involved technologies.Your
PHPscript will work, I have tested it on my local Apache server. Although there are some oddities; Such as inserting anhtml<br/>intoXML.XMLis a data format(At least in this context), not really intended for presentation. Yourhtmlfile also works fine.There are three super-obvious ways to go from where you are.
1) Do what your above comment says. Use a script to write to a database then use another to access it. The first from a webpage the second from objective-c code. I’m fairly sure this is not something you want to try right now.
2) Interrupt the sending of the
postfrom aUIWebViewin your application. Send that data to your server by other means and parse the results.3) Ignore the web-view and just send the arguments to the server directly.
Both 2 & 3 are very related. The main difference is the origin of the
NSURLRequest.For 2, obtaining the post request is pretty easy. First you would set some object, let’s say your view controller, as the web-view’s delegate. Then implement the following method.
For 3, You simply create the
NSURLRequestyourself and configure it for your needs. This requires theNSMutableURLRequestsubclass.And then in either case (2 or 3) you can use
requestto load the data to be parsed. Here is, for example, a very bad (synchronous) way to load the data.This data can then be fed to a parser or for testing simply be logged: