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
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".

 

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.