error_reporting(E_ALL); ini_set("display_errors", 1); $handle = @fopen("site_hierarchy.txt", "r"); if ($handle) { /** Creating the root element in the xml **/ $doc = new DOMDocument("1.0"); // Read File Line By Line $prevCount = -1; $prevNode = NULL; $prevNode = $doc->createElement("root"); $doc->appendChild($prevNode); while (($fileContent = fgets($handle, 4096)) !== false) { $count = 0; $newStrLine = $fileContent; $newStrLine = str_replace("\t","" , $newStrLine, $count); $child = createTerms($doc,$newStrLine); if ( $prevCount < $count ) { $prevNode = $prevNode->appendChild($ch