You are here
Generating xml from text file
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($child); } else if ( $prevCount == $count ) { $prevNode->parentNode->appendChild($child); } else if ( $prevCount > $count ) { while( $prevCount >= $count ) { $prevNode = $prevNode->parentNode; $prevCount--; } $prevNode->appendChild($child); } $prevNode = $child; $prevCount = $count; } } $xml = $doc->saveXML(); echo $xml; $f = fopen("test.xml", "w"); fwrite($f, $xml); fclose($f); /** Function to clean the titles **/ function clean_title($string) { $cleanString = htmlspecialchars_decode( strtolower(trim($string)), ENT_QUOTES ); $cleanString = str_replace(array("& ", "'","\"",":",",",".","0","*","(",")","&","\t"), '', $cleanString); return str_replace(' ', '_', $cleanString ); } function str_replace_once($needle, $replace, $haystack){ $pos = strpos($haystack, $needle); if ($pos === false) { return $haystack; } return substr_replace($haystack, $replace, $pos, strlen($needle)); } /** Create term tags **/ function createTerms($doc,$newStrLine){ $child = $doc->createElement("term"); $name = $doc->createElement("name"); $name->appendChild($doc->createCDATASection(str_replace('0','',$newStrLine))); $weight = $doc->createElement("weight"); $weight->appendChild($doc->createCDATASection("0")); $description = $doc->createElement("description"); $description->appendChild($doc->createCDATASection("description")); $machine_name = $doc->createElement("machine_name"); $machine_name->appendChild($doc->createCDATASection("sidebar_menu_root")); $target_link = $doc->createElement('target_link'); $target_link->appendChild($doc->createCDATASection("")); $item_type = $doc->createElement('item_type'); $item_type->appendChild($doc->createCDATASection("")); $product_category = $doc->createElement('product_category'); $product_category->appendChild($doc->createCDATASection("")); $special_options = $doc->createElement('special_options'); $special_options->appendChild($doc->createCDATASection("")); $display_title = $doc->createElement('display_title'); $display_title->appendChild($doc->createCDATASection("display_title")); $hide = $doc->createElement('hide'); $hide->appendChild($doc->createCDATASection("0")); $hide_breadcrumb_menu = $doc->createElement('hide_breadcrumb_menu'); $hide_breadcrumb_menu->appendChild($doc->createCDATASection("0")); $hide_breadcrumb = $doc->createElement('hide_breadcrumb'); $hide_breadcrumb->appendChild($doc->createCDATASection("1")); $hide_sitemap = $doc->createElement('hide_sitemap'); $hide_sitemap->appendChild($doc->createCDATASection("0")); $icon = $doc->createElement('icon'); $icon->appendChild($doc->createCDATASection("dummy.jpg")); /** Appending the tag names **/ $child->appendChild($name); $child->appendChild($weight); $child->appendChild($machine_name); $child->appendChild($target_link); $child->appendChild($item_type); $child->appendChild($product_category); $child->appendChild($special_options); $child->appendChild($display_title); $child->appendChild($hide); $child->appendChild($hide_breadcrumb_menu); $child->appendChild($hide_breadcrumb); $child->appendChild($hide_sitemap); $child->appendChild($icon); return $child; } function count_words($string){ $word_array = explode("0", $string); $num = count($word_array); return $num; }