Simple XML Parser

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
fangonk
Forum Newbie
Posts: 21
Joined: Thu May 14, 2009 6:43 am

Simple XML Parser

Post 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!
Last edited by Benjamin on Thu May 14, 2009 12:12 pm, edited 1 time in total.
Reason: Changed code type from text to php.
lettie_dude
Forum Commoner
Posts: 65
Joined: Thu Dec 07, 2006 10:10 am

Re: Simple XML Parser

Post 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.
fangonk
Forum Newbie
Posts: 21
Joined: Thu May 14, 2009 6:43 am

Re: Simple XML Parser

Post by fangonk »

I've added the file with the correct name and I am still seeing no difference.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Simple XML Parser

Post by Darhazer »

You are trying to write to the root directory (/) and obviously you have no permissions
Change

Code: Select all

$fp = fopen('/myxml.xml', 'w');
To

Code: Select all

$fp = fopen('./myxml.xml', 'w');
Or

Code: Select all

$fp = fopen(dirname(__FILE__).'/myxml.xml', 'w'); // to ensure the xml will be in the same folder as PHP
Last edited by Benjamin on Thu May 14, 2009 4:23 pm, edited 1 time in total.
Reason: Changed code type from text to php.
fangonk
Forum Newbie
Posts: 21
Joined: Thu May 14, 2009 6:43 am

Re: Simple XML Parser

Post 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?
ldougherty
Forum Contributor
Posts: 103
Joined: Sun May 03, 2009 11:39 am

Re: Simple XML Parser

Post 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'>";
?>
 
 
Last edited by Benjamin on Thu May 14, 2009 8:19 pm, edited 1 time in total.
Reason: Changed code type from text to php.
fangonk
Forum Newbie
Posts: 21
Joined: Thu May 14, 2009 6:43 am

Re: Simple XML Parser

Post 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>
 
 
Last edited by Benjamin on Thu May 14, 2009 8:19 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Simple XML Parser

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
ldougherty
Forum Contributor
Posts: 103
Joined: Sun May 03, 2009 11:39 am

Re: Simple XML Parser

Post 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.
fangonk
Forum Newbie
Posts: 21
Joined: Thu May 14, 2009 6:43 am

Re: Simple XML Parser

Post 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.
fangonk
Forum Newbie
Posts: 21
Joined: Thu May 14, 2009 6:43 am

Re: Simple XML Parser

Post by fangonk »

Wait I got it! I used PHP's header redirect function. Thanks everyone for the help! I really appreciate it.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Simple XML Parser

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Simple XML Parser

Post by onion2k »

Why are you using XML?
Post Reply