Scarby Posted April 6, 2008 Share Posted April 6, 2008 i'm currently trying to write a web page that using php outputs a tree of project elements and do do this using a recursive function however i am having problems that i do not quite understand. The code to me looks ok however once uploaded to a web server it has problems if anyone can spot the problem that would be much appreciated. the error given on the server is: Warning: DOMXPath::query() expects parameter 2 to be DOMNode, object given in /home/scarby/public_html/XML/projects.php on line 25 Warning: Invalid argument supplied for foreach() in /home/scarby/public_html/XML/projects.php on line 28 the XHTML/PHP code is: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <link href="main.css" rel="stylesheet" type="text/css" /> </head> <body> <table name=projects> <?php /* page designed to display all projects within an XML file currently broken TODO: find a method of tracking recursion */ //declare function arguments taken are context node and the xpath object (xpath object to avoid issues with function scope) function queryecho($context, $xpath) { //declare query $query= '/project'; //run run query $projects = $xpath->query($query, $context); //loop through nodes returned by query and create output foreach ($projects as $project) { echo "<tr><td> {$project->firstChild->nodeValue} <button name ='{$project->firstChild->nodeValue}' onclick=''>Edit</button> </td></tr> \n"; //if project element has child nodes if ($project->haschildnodes()) { queryecho($project, $xpath); } } } //initialise dom, load xml file then initialise xpath $doc = new DOMDocument(); $doc->preserveWhiteSpace = false; $doc->load('projects.xml'); $xpath = new DOMXPath($doc); //initially find context node then pass to function $projects=$doc->getElementsByTagName(projects); queryecho($projects, $xpath, 0); </table> the XML code for reference is though this is dynamic and will change so it is important that the function is recursive/some form of loop is used: <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <?xml-stylesheet type="text/css" href="Projects.css" ?> <!DOCTYPE projects SYSTEM "projects.dtd"> <projects> <project status="active" name="Administration"> <spoken_name>Administration</spoken_name> <directory_name>Administration</directory_name> <project status="inactive" name="Computer Setup"> <spoken_name>Computer Setup</spoken_name> <directory_name>ComputerSetup</directory_name> </project> </project> <project status="active" name="Teaching"> <spoken_name>Teaching</spoken_name> <directory_name>Teaching</directory_name> <project status="inactive" name="XML and the Web"> <spoken_name>XML and the Web</spoken_name> <directory_name>XMLandtheWeb</directory_name> </project> </project> </projects> Link to comment https://forums.phpfreaks.com/topic/99842-php-xml-recursive-function-trouble/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.