-1

I am trying to dynamically create XML but i've run into a snag, for somereason it's automatically encoding the < and >, i need it to stop that because it's breaking my tags but i can't see anywhere in my function it's tell it to encode.

function GenerateList($titleB, $descB, $thumbB, $dirB, $patternB){
if (is_dir($dirB)){
$myDirectory = opendir($dirB);
// get each entry
while($entryName = readdir($myDirectory)) {
    $dirArray[] = $entryName;
}

// close directory
closedir($myDirectory);

//  count elements in array
$indexCount = count($dirArray);
print ("<center><h1>'$titleB' Directory</h1>");
$true_count = 0;

// sort em
sort($dirArray);
$outputB = "<CATEGORY name=\"$titleB\" desc=\"$descB\" thumb=\"$thumbB\">";
print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>");
print("<TR><TH>Filename</TH><th>Filesize</th><TH>Filename</TH><th>Filesize</th><TH>Filename</TH><th>Filesize</th></TR>");
print("<TR><TD colspan=2><TABLE border=0 cellpadding=0 cellspacing=0 class=whitelinks>");
// loop through the array of files and print them all
$one_third = round($indexCount/3);
$two_third = $one_third+$one_third;
for($index=0; $index < $indexCount; $index++) {
    $ext = explode(".", $dirArray[$index]);
    $parsed_title = preg_replace ($patternB, "", $ext[0]);
    if ((substr("$dirArray[$index]", 0, 1) != ".")&&($ext[1] == "flv")){ // don't list hidden files
        $true_count++;
        print("<TR><TD><a href=\"$dirB$dirArray[$index]\">$parsed_title</a></td>");
        print("<td>");
        print(filesize($dirB.$dirArray[$index]));
        print("</td>");
        print("</TR>");
            if ($one_third == ($index+1) || $two_third == ($index+1)){
                print("</td></TR></table>");
                print("</TD><TD colspan=2><TABLE border=0 cellpadding=0 cellspacing=0 class=whitelinks>");
            }
        $outputB .= "<ITEM>";
        $outputB .= "<file_path>/$dirB".htmlentities($dirArray[$index])."</file_path>";
        $outputB .= "<file_width>500</file_width>";
        $outputB .= "<file_height>375</file_height>";
        $outputB .= "<file_title>".$parsed_title."</file_title>";
        $outputB .= "<file_desc>Loaded from a seperate txt file, index to match with the index of the dir file</file_desc>";
        $outputB .= "<file_image>$thumbB</file_image>";
        $outputB .= "<featured_image>$thumbB</featured_image>";
        $outputB .= "<featured_or_not>true</featured_or_not>";
        $outputB .= "</ITEM>";
    }
}
print("</td></TR></table>");
print("</TR><td colspan=6 align=right>$true_count files</td></TABLE>");
};//if (file_exists($dirB))
$outputB .= "</CATEGORY>";
return $outputB;
};//function

then i take the final output of several function calls and pass it to the xml parser

//$output = concatoniated returns of several GenerateList functions $dom = new DOMDocument('1.0', 'utf-8');

$element = $dom->createElement('CONTENT',$output);

$dom->appendChild($element);
  $xml_final = $dom->saveXML(); 
  $dom->save("playlist.xml") 
animuson
  • 50,765
  • 27
  • 132
  • 142
NekoLLX
  • 175
  • 5
  • 17
  • It's doing waht you told it to. If you are going to build the xml up as string, just save the entire thing to a file with .xml as an extension. Other way is to ceate an xmlDocument and the load the string to it. Or depending on waht xml library you are using, there might be an InnerXml property as well as InnerText – Tony Hopkinson Jul 27 '12 at 18:45
  • i'm pretty sure that's what i was doing "create a xml document and load the string into it" $element = $dom->createElement('CONTENT',$output); $dom->appendChild($element); $xml_final = $dom->saveXML(); $dom->save("playlist.xml") – NekoLLX Jul 28 '12 at 17:47
  • Nope you are adding an element. Anything in $output in the CreateElement call will be escaped and marked up with a CONTENT tag. Not going to work – Tony Hopkinson Jul 28 '12 at 20:13

1 Answers1

0

PHP isn't my thing but to get you on your way.

DOMDocument should have a method something like LoadXml.

So you need to put a bit more in your $output, ?xml tag and a root node etc, then create a DoMDocument and call LoadXml($output)

Or something similar any way.

Tony Hopkinson
  • 19,528
  • 3
  • 29
  • 38
  • Thanks that did it! $dom = new DOMDocument('1.0', 'utf-8'); $output = "".$output.""; $dom->loadXML($output); $xml_final = $dom->saveXML(); $dom->save("playlist.xml") – NekoLLX Jul 28 '12 at 21:37