Page 1 of 1
Simple XML Parser
Posted: Thu May 14, 2009 6:49 am
by fangonk
I am trying to create a really simple xml parser. I want to create xml files from scratch based on what a user puts in to a basic html form. Here is what I have so far:
makexml.php:
Code: Select all
<?php
$str = $_POST['name'];
$xml = "<?xml version='1.0' encoding='UTF-8'?>
<menu>
<item value=".$str.">
</menu>";
$fp = fopen('/myxml.xml', 'w');
fwrite($fp, $xml);
fclose($fp);
?>
index.html:
Code: Select all
<html>
<head>
</head>
<body>
<form action="/makexml.php" method="post">
<label for="item">Text: </label><input type="text" name="name" id="name" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
All permisions are set to 777, I have PHP5 installed and I am getting the following error:
Warning: fopen(/myxml.xml) [function.fopen]: failed to open stream: Read-only file system in /makexml.php on line 9
Warning: fwrite(): supplied argument is not a valid stream resource in /html/makexml.php on line 10
Warning: fclose(): supplied argument is not a valid stream resource in /html/makexml.php on line 11
No file is being created.
I am really new to PHP, so I am sure that my error must be pretty simple. Help!
Re: Simple XML Parser
Posted: Thu May 14, 2009 7:52 am
by lettie_dude
Your code is looking for an .xml file on the server. You need to upload a blank .xml file with the file name you have chosen.
Re: Simple XML Parser
Posted: Thu May 14, 2009 8:33 am
by fangonk
I've added the file with the correct name and I am still seeing no difference.
Re: Simple XML Parser
Posted: Thu May 14, 2009 3:45 pm
by Darhazer
You are trying to write to the root directory (/) and obviously you have no permissions
Change
To
Or
Code: Select all
$fp = fopen(dirname(__FILE__).'/myxml.xml', 'w'); // to ensure the xml will be in the same folder as PHP
Re: Simple XML Parser
Posted: Thu May 14, 2009 7:28 pm
by fangonk
Hey that worked really well! Thanks a lot.
I have one follow up question:
I'm want to load the contents of the xml file in to the text field in the original html file. So if the xml field reads: 'Hello World!' the text field in the original HTML file looks like:
<input type="text" name="name" id="name" value="Hello World!"/>
Is there a way to do this using php? If so, what is the best way to set that up?
Re: Simple XML Parser
Posted: Thu May 14, 2009 7:49 pm
by ldougherty
Simple enough, just assign the content of the xml file to a variable and then use the variable on the input field.
Code: Select all
<?php
$myvar = file_get_contents('./myxml.xml');
echo "<input type='text' name='name' id='name' value='$myvar'>";
?>
Re: Simple XML Parser
Posted: Thu May 14, 2009 7:53 pm
by fangonk
So can I just parse that directly in to an html file?
Code: Select all
<html>
<head>
<link rel="stylesheet" href="styles.css" type="text/css" media="screen" />
<title></title>
</head>
<body>
<form action="/makexml.php" method="post">
<label for="item">Text: </label>
<?php
$myvar = file_get_contents('./myxml.xml');
echo "<input type='text' name='name' id='name' value='$myvar'>";
?>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
Re: Simple XML Parser
Posted: Thu May 14, 2009 7:58 pm
by s.dot
Check out the SimpleXML() class
http://www.php.net/simplexml
It will allow you to traverse your XML document and pull out what you need.
Re: Simple XML Parser
Posted: Thu May 14, 2009 7:59 pm
by ldougherty
Well that would depend if HTML is being parsed through the PHP interpreter.
If it is then the command should work, if it is not then it will likely do nothing.
Re: Simple XML Parser
Posted: Thu May 14, 2009 8:13 pm
by fangonk
Thanks everybody, I renamed my html file with .php extension and it worked fine. One last newbie question( I swear it!): is there a way to define where the browser goes after a user hits the submit button?
Basically, the way I have the script set up now, it goes to 'makexml.php' and I just want form page to refresh.
Re: Simple XML Parser
Posted: Thu May 14, 2009 8:53 pm
by fangonk
Wait I got it! I used PHP's header redirect function. Thanks everyone for the help! I really appreciate it.
Re: Simple XML Parser
Posted: Fri May 15, 2009 5:32 pm
by Darhazer
To display it correctly, use:
Code: Select all
$myvar = htmlspecialchars( file_get_contents('./myxml.xml') );
echo "<input type='text' name='name' id='name' value='$myvar'>";
because without htmlspecialchars it will work if .xml contains "Hello world", but if there is a ' character, it will break the HTML and will display the rest of the content outside of the text field.
Re: Simple XML Parser
Posted: Fri May 15, 2009 5:47 pm
by onion2k
Why are you using XML?