Jump to content

How do i output this php as xml?


sleazyfish

Recommended Posts

Hi,

 

This script lists all the contents of the dir it is placed in bar itself. Perfect for what I need. However, I want to output the results as xml and being new to php I really don't know where to start.

 

<?php
foreach (new DirectoryIterator('.') as $file) {
     if ( (!$file->isDot()) && ($file->getFilename() != basename($_SERVER['PHP_SELF'])) ) {
            echo ($file->isDir()) ? "(Dir) ".$file->getFilename() : $file->getFilename();
         }
}
?>

 

I'd be thankful for the help

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/219167-how-do-i-output-this-php-as-xml/
Share on other sites

Sorry I should have thought to post an example in my original query.

 

This would be to desired format:

 

<?xml version="1.0" encoding="UTF-8" ?> 
- <images>
  <image>file1</image> 
  <image>file2</image> 
  <image>file3</image> 
  <image>file4</image> 
  <image>file5</image> 
  </images>

 

If possible i would prefer the output to exclude the extension of the file e.g "file1.jpg" becomes just "file1".

 

There are many ways of writing XML in PHP, could you take a look over some and see which you like most. For your needs, they're all about as easy to get started with as each other.

 

So, for reference:

 

Document Object Model (DOM)

http://php.net/dom

SimpleXML

http://php.net/simplexml

XML Writer

http://php.net/xmlwriter

 

And for getting the file name without the extension, you could

a) get the file extension, then

b) use $file->getBasename($ext).

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.