Jump to content

Recommended Posts

I am generating XML with a recursive folder function. I am not able to close out my project node for some reason.

<?php
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<structure>\n";

getDirectory("clients/xyzclient1"); 

function getDirectory($path = '.', $level = 0)
{ 
// Directories to ignore when listing output. Many hosts
// will deny PHP access to the cgi-bin.
    $ignore = array('cgi-bin', '.', '..');
    // Open the directory to the handle $dh
    $dh = @opendir($path);
    // Loop through the directory
    while (false !== ($file = readdir($dh)))
{
        // Check that this file is not to be ignored
        if (!in_array($file, $ignore))
	{
            // Just to add spacing to the list, to better
            // show the directory tree.
            $spaces = str_repeat(' ', ($level * 4 ));
            // Its a directory, so we need to keep reading down...
            if (is_dir("$path/$file"))
		{
                // Re-call this same function but on a new directory.
                // this is what makes function recursive.
                echo "\t<project name=\"$file\">\n";
                getDirectory("$path/$file", ($level+1));
            }
		else
		{
                // Just print out the filename
                echo "\t\t<file>$file</file>\n";
            }
        }
    }
    // Close the directory handle
    closedir($dh);
}
echo "</structure>";
?> 

 

That is outputting this:

<?xml version="1.0" encoding="utf-8"?>
<structure>
<project name="project2">
	<file>edit_client_info.png</file>
	<file>loading16x16listFM.gif</file>
	<file>nice interface.psd</file>
<project name="project1">
	<file>edit_client_info.png</file>
	<file>loading16x16listFM.gif</file>
	<file>nice interface.psd</file>
</structure> 

 

Any ideas what I need to change?

ok I think I understand it, but I am getting an error:

Fatal error: Call to a member function appendChild() on a non-object on line 40

 

my code:

<?php
header("content-type: application/xml; charset=ISO-8859-15");

$xml = new DOMDocument("1.0", "ISO-8859-15");
$xml_structure = $xml->createElement("structure");
getDirectory("clients/xyzclient1"); 

function getDirectory($path = '.', $level = 0)
{ 
// Directories to ignore when listing output. Many hosts
// will deny PHP access to the cgi-bin.
    $ignore = array('cgi-bin', '.', '..');
    // Open the directory to the handle $dh
    $dh = @opendir($path);
    // Loop through the directory
    while (false !== ($file = readdir($dh)))
{
        // Check that this file is not to be ignored
        if (!in_array($file, $ignore))
	{
            // Just to add spacing to the list, to better
            // show the directory tree.
            $spaces = str_repeat(' ', ($level * 4 ));
            // Its a directory, so we need to keep reading down...
            if (is_dir("$path/$file"))
		{
                // Re-call this same function but on a new directory.
                // this is what makes function recursive.
			$xml_project = $xml->createElement("project");
			$xml_project->setAttribute("name", "$file"); //<---- LINE 40
                getDirectory("$path/$file", ($level+1));
            }
		else
		{
                // Just print out the filename
			$xml_file = $xml->createElement("file","$file");
               // echo "\t\t<file>$file</file>\n";
            }
        }
	$xml_project->appendChild( $xml_file );
	$xml_structure->appendChild( $xml_project );
    }
    // Close the directory handle
$xml->appendChild( $xml_structure );
print $xml->saveXML();
    closedir($dh);
}

function directory2xml(&$xmlRoot, $directory, $recursive = true, $ignore = array()) {
    if (!is_dir($directory) || !is_readable($directory)) return false;
    $files = scandir($directory);
    foreach ($files as $file) {
        if ('.' === $file[0] || in_array($file, $ignore)) continue;
        $fullpath = implode(DIRECTORY_SEPARATOR, array($directory, $file));
        if (is_file($fullpath)) {
            $xmlRoot->appendChild($xmlRoot->createElement('file', $fullpath));
        } else if ($recursive) {
            $project = $xmlRoot->createElement('project');
            $project->setAttribute('name', $file);
            directory2xml($project, $fullpath, $recursive, $ignore);
        }
    }
}

$dom = new DomDocument('1.0', 'ISO-8859-1');
$root = $dom->appendChild($dom->createElement('structure'));
directory2xml($root, realpath('path/to/directory'), true, array('cgi-bin'));

print $dom->saveXml();

I appreciate your help so far ignace. I am hoever getting a new error:

Fatal error: Call to undefined method DOMElement::createElement() on line 13

 

Line 13 is this line:            $project = $xmlRoot->createElement('project');

 

Does this dom stuff come with PHP or is it an extension?

 

edit::scratch that my server has DOM/XML enabled

Try:

 

function directory2xml(&$xmlRoot, $directory, $recursive = true, $ignore = array()) {
    if (!is_dir($directory) || !is_readable($directory)) return false;
    $files = scandir($directory);
    foreach ($files as $file) {
        if ('.' === $file[0] || in_array($file, $ignore)) continue;
        $fullpath = implode(DIRECTORY_SEPARATOR, array($directory, $file));
        if (is_file($fullpath)) {
            $xmlRoot->appendChild(new DomElement('file', $fullpath));
        } else if ($recursive) {
            $project = new DomElement('project');
            $project->setAttribute('name', $file);
            directory2xml($project, $fullpath, $recursive, $ignore);
        }
    }
}

$dom = new DomDocument('1.0', 'ISO-8859-1');
$root = $dom->appendChild($dom->createElement('structure'));
directory2xml($root, realpath('path/to/directory'), true, array('cgi-bin'));

print $dom->saveXml();

A new error arises  :shrug: ...this one is a doozy

Fatal error: Uncaught exception 'DOMException' with message 'No Modification Allowed Error' in /homepages/26/d220262095/htdocs/flashden/clientlogin2/directory.php:14
Stack trace:
#0 /homepages/26/d220262095/htdocs/flashden/clientlogin2/directory.php(14): DOMElement->setAttribute('name', 'project1')
#1 /homepages/26/d220262095/htdocs/flashden/clientlogin2/directory.php(22): directory2xml(Object(DOMElement), '/homepages/26/d...', true, Array)
#2 {main} thrown in /homepages/26/d220262095/htdocs/flashden/clientlogin2/directory.php on line 14

 

Line 14 is: $project->setAttribute('name', $file);

 

A new error arises  :shrug: ...this one is a doozy

Fatal error: Uncaught exception 'DOMException' with message 'No Modification Allowed Error' in /homepages/26/d220262095/htdocs/flashden/clientlogin2/directory.php:14
Stack trace:
#0 /homepages/26/d220262095/htdocs/flashden/clientlogin2/directory.php(14): DOMElement->setAttribute('name', 'project1')
#1 /homepages/26/d220262095/htdocs/flashden/clientlogin2/directory.php(22): directory2xml(Object(DOMElement), '/homepages/26/d...', true, Array)
#2 {main} thrown in /homepages/26/d220262095/htdocs/flashden/clientlogin2/directory.php on line 14

 

Line 14 is: $project->setAttribute('name', $file);

 

 

Woeps sorry:

 

function directory2xml(&$xmlRoot, $directory, $recursive = true, $ignore = array()) {
    if (!is_dir($directory) || !is_readable($directory)) return false;
    $files = scandir($directory);
    foreach ($files as $file) {
        if ('.' === $file[0] || in_array($file, $ignore)) continue;
        $fullpath = implode(DIRECTORY_SEPARATOR, array($directory, $file));
        if (is_file($fullpath)) {
            $xmlRoot->appendChild(new DomElement('file', $fullpath));
        } else if ($recursive) {
            $project = $xmlRoot->appendChild(new DomElement('project'));
            $project->setAttribute('name', $file);
            directory2xml($project, $fullpath, $recursive, $ignore);
        }
    }
}

$dom = new DomDocument('1.0', 'ISO-8859-1');
$root = $dom->appendChild($dom->createElement('structure'));
directory2xml($root, realpath('path/to/directory'), true, array('cgi-bin'));

print $dom->saveXml();

 

Check if that works I know what the problem is not sure this solves it.

success!  ;) You are awesome man. I appreciate it so much!

 

One question..How do I get rid of the full path thing?

 

The file node comes out like this:

/homepages/26/d220262095/htdocs/flashden/clientlogin2/clients/xyzclient1/project1/edit_client_info.png

 

and my webroot is htdocs. What I would like it to be is just 'xyzclient1/project1/edit_client_info.png'

 

Thanks again!

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.