FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


The best way to dynamically transform XML





novisdesign
Hello,
I am creating a poll for my web site. I have planned to use XML to hold the polls data. I need an easy way to read the xml, make changes, then save the XML file. I don't know the best way to do this. Probably PHP, or is there another way?

Thanks
-gs
yjwong
Yes, you can use PHP to manipulate XML files. You can just use the XML parser functions/xmlreader for reading the XML files, and use File Operation Functions/xmlwriter to write the XML files. For more information please visit the following URL: http://www.php.net/xml
novisdesign
Thanks man, ill check that out.
novisdesign
I was not able to get those functions to do anything on my server running PHP 5.1. Do I need to install something extra?

Thanks
SlowWalkere
Which functions did you have trouble with?

I think the XML functions are installed by default. The php.net page talks about installing something, but it sounds like it's bundled with Apache and so no further installation is necessary. But, I've never installed the stuff on my own computer (I just use the frihost server for development/testing).

The file functions should definitely work, as they are basic parts of php.

Maybe you should try running your script on the server, since it's set up properly. If it works there and not on your machine, then you probably need to install something extra.

- Walkere
novisdesign
Here is a file of exactly what the php.net example shows. Example. Please tell me what im doing wrong.
SlowWalkere
Which example is it? If you mean the first example on the XML-info page ("Show XML element structure"), I just copied and pasted it and it ran fine for me. Or, if you're talking about a different example, which one?

Either way, post the source here, so we can see what's causing the error.

- Walkere
novisdesign
Here is the source of the php:

Code:

<?php
$file = "blog.xml";
$depth = array();

function startElement($parser, $name, $attrs)
{
   global $depth;
   for ($i = 0; $i < $depth[$parser]; $i++) {
       echo "  ";
   }
   echo "$name\n";
   $depth[$parser]++;
}

function endElement($parser, $name)
{
   global $depth;
   $depth[$parser]--;
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
if (!($fp = fopen($file, "r"))) {
   die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
   if (!xml_parse($xml_parser, $data, feof($fp))) {
       die(sprintf("XML error: %s at line %d",
                   xml_error_string(xml_get_error_code($xml_parser)),
                   xml_get_current_line_number($xml_parser)));
   }
}
xml_parser_free($xml_parser);
?>


Hope you guys can see whats wrong.

-gs
SlowWalkere
Well, that's awful weird. I don't know what's wrong.

Is that website hosted on frihost or a different host? Because I tried it on frihost's server 2 and it worked fine. I also tried it on another server with php 5.1, and it worked.

However, I looked around a little more for installing xml with php, and it looks like the problem might be that it's not enabled. It was a bit unclear on the php page, since it said that the necessary library was included with apache. It turns out that it's included, but you don't have to install it as part of php.

Also, it looks like the phpinfo page has a section on xml support. Looking at frihost's phpinfo page, there's a small section about 80% of the way down the page titled "XML" with three entries - XML Support, XML Namespace Support, and EXPAT Version. Check the phpinfo on your server and maybe it will tell you if xml is disabled.

- Walkere
novisdesign
The site is hosted here on Frihost and all of the xml php things are enabled. Can't tell what's going wrong. If anybody know please help.

-gs
Related topics
The best site for learning JavaScript
NotePad Replacements
Which Linux distribution is the best?
Rotate images using css?
[man]Photoshop: Tutos en español.
Feasability question
CSS or XSL
MySql or MsSql?
XML and using DOM or SAX parser
Newb flash and xml question
Best PHP FRAMEWORK
Creating XML files using PHP
best programming editor
Bak in a flash! flash stuff for frih (nothing is impossible)
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.