xml php table

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
myitalan
Forum Newbie
Posts: 2
Joined: Sat Jul 14, 2012 3:05 pm

xml php table

Post by myitalan »

Greetings,

I have an issue i hope you can help me with regarding parsing an xml file to table in php.


1. I have an input form that writes an xml file. (it works)

2. the xml file it writes is in this format:

<?xml version="1.0" encoding="iso-8859-1"?>

<images>
<title>2</title>
<description>2</description>
<tmg></tmg>
<img>2</img>
</images>

3. Now, this is what I need for my flash gallery to play and I can't change that.

4. The Problem is when I list all the images in the admin section to add and delete, I am getting a parsing error.

- Clue the parser will work if I format the xml file like this:

<images>
<image title="2" description="2" tmb="2" img="2" />
</images>

5. Finally, yes I would write to that format using:
<?php
$write_string = "<images>";
foreach($images_final as $image){
$write_string .= "<image title=\"$image[title]\" description=\"$image[description]\" tmb=\"$image[tmb]\" img=\"$image[img]\" />";
}
$write_string .= "</images>";
$fp = fopen("gallery.xml", "w+");
fwrite($fp, $write_string) or die("Error writing to file");
fclose($fp);
?>
....but my flash gallery won't work.



Here is what I'm using that is generating the error.

<?php
function start_tag($parser, $name, $attrs){
global $table_string;
$table_string .= "<tr><td>[title]</td><td>[description]</td><td>[tmb]</td><td>[img]</td></tr>";
}
function end_tag($parser, $name){}
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_set_element_handler($parser, "start_tag", "end_tag");
$table_string = "<table>
<tr><th>Title</th><th>Description</th><th>Thumbnail</th><th>Image</th></tr>";
xml_parse($parser, file_get_contents("gallery.xml")) or die("Error parsing XML file");
$table_string .= "</table>";
echo $table_string;

?>

Thank you in advance for your help!!



Alan

Reply With Quote
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: xml php table

Post by tr0gd0rr »

Could any of the items or attributes have unescaped entities? e.g. ampersands that should be `&` instead of `&`
You can try DOMDocument as well. I think it is the preferred way to parse XML or HTML.
Post Reply