Jump to content

Can I modify an XML document with PHP


poleposters

Recommended Posts

Hi all,

 

I have a flash page flip script which gets all the image data from an XML document.

 

Is it possible to populate this document with PHP?

 

I want to add it on to my users profile pages so that they can have a page flip photo album dynamically created from their images in my database.

 

I've got most of the PHP basics down, but XML is new to me. I'm going to learn what I can, but in the mean time any advice would be appreciated.

 

Cheers.

Link to comment
https://forums.phpfreaks.com/topic/101728-can-i-modify-an-xml-document-with-php/
Share on other sites

Yes, you can do it using fopen, fwrite, and fclose.

 

Basically you build a string containing your XML:

 

$string = "<indexes>" . PHP_EOL;
$string .= "<index>" .PHP_EOL;
$string .= "<element>element data</element>" . PHP_EOL;
$string .= "</index>" . PHP_EOL;
$string .= "</indexes>". PHP_EOL;

 

The PHP_EOL causes a line break that makes your XML easier to read. You can leave them out if you want, they are not essential.

 

Then, you open a file, write to it, and close the file:

 

 $open = fopen("file.xml", "w");
fwrite ($open, $string);
fclose($open);

 

This is super simplified, not complete (no doctype on the xml file etc), and not tested, so there may be some errors in there. But the idea is to show you how to do it, not write the code for you. Good luck, and post back here if you are having some troubles.

Great!

 

I've created my first file, But since I have to create a file for each registered user I need to give the file an individual name probably based on the user id. ie 35_file.xml

 

Is this possible? Also what is the significance of  "w" in the following line?

 

 $open = fopen("file.xml", "w");

Thank you. Didn't read carefully enough.

 

I've tried renaming the file to be created with partial success.

 

This is what I've tried.

$businessid=35
$open = fopen("xml/$businessid._.file.xml", "w");

 

except the file created is 35._.file.xml.

What I want to do is  name the file is 35_file.xml

 

Any ideas?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.