2012-07-27 43 views
-1

我想動態創建XML,但我遇到了一個障礙,因爲somereason它自動編碼<和>,我需要它來阻止它,因爲它打破了我的標籤,但我看不見在我的功能的任何地方它告訴它編碼。XML自動編碼

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 

然後我採取若干函數調用最終輸出並將其傳遞給XML解析器

// $輸出=幾個GenerateList功能concatoniated返回 $ DOM =新的DOMDocument( '1.0', 'UTF-8');

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

$dom->appendChild($element); 
    $xml_final = $dom->saveXML(); 
    $dom->save("playlist.xml") 
+0

這是做你要告訴它。如果您打算將xml構建爲字符串,只需將整個文件保存爲以.xml作爲擴展名的文件即可。其他方法是cemented一個xmlDocument並將字符串加載到它。或者根據你正在使用的xml庫,可能有一個InnerXml屬性以及InnerText – 2012-07-27 18:45:37

+0

我很確定這就是我正在做的「創建一個xml文檔並將字符串加載到它」 $ element = $ dom- >的createElement( '內容',$輸出); $ dom-> appendChild($ element); $ xml_final = $ dom-> saveXML(); $ dom-> save(「playlist.xml」) – NekoLLX 2012-07-28 17:47:08

+0

不添加元素。 CreateElement調用中的$ output中的任何內容都將被轉義並用CONTENT標記標記。不去上班 – 2012-07-28 20:13:33

回答

0

PHP是不是我的事情,但要讓你的方式。

DOMDocument應該有一個類似於LoadXml的方法。

所以你需要把更多的在$輸出,?XML標籤和根節點等,然後創建一個DOMDocument和調用的loadXML($輸出)

或者類似的任何方式。

+0

謝謝你做到了! $ dom = new DOMDocument('1.0','utf-8'); $ output =「」。$ output。「」; $ dom-> loadXML($ output); $ xml_final = $ dom-> saveXML(); $ dom-> save(「playlist.xml」) – NekoLLX 2012-07-28 21:37:04