2012-10-17 63 views
0

我想通過我的MySQL數據進行循環,併爲我的XML清理它,但我試圖以最有效的方式執行此操作。以下是我的代碼到目前爲止。此代碼僅清理一個字段CompanyName如果可能,我想清除所有8個字段(如果相同的循環)。清潔XML的XML數據

header("Content-type: text/xml"); 
$SQL_query = "SELECT * FROM COMPANYINFO ORDER BY CompanyName DESC"; 
$resultID = mysql_query($SQL_query, $linkID) or die("Data not found."); 

$xml_output = "<?xml version=\"1.0\"?>\n"; 
$xml_output .= "<markers>\n"; w 

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){ 
    $row = mysql_fetch_assoc($resultID); 

      // Escaping illegal characters 
     $row['CompanyName'] = str_replace("&", "&", $row['CompanyName']); 
     $row['CompanyName'] = str_replace("<", "<", $row['CompanyName']); 
     $row['CompanyName'] = str_replace(">", "&gt;", $row['CompanyName']); 
     $row['CompanyName'] = str_replace("\"", "&quot;", $row['CompanyName']); 

    $xml_output .= "\t\t<marker name='.$row['CompanyName']."' address='".$row['Address_1']."' phone='".$row['Phone']."' lat='".$row['Lat_Info']."' lng='".$row['Long_Info']."' county='".$row['County']."'/>\n"; 
} 

$xml_output .= "</markers>"; 


echo $xml_output; 

回答

6

運行任何需要通過htmlspecialchars()放入XML的字符串,你應該沒問題。

至於處理結果,您可以使用foreach循環遍歷數組。

放在一起,你會得到:

foreach($row as $column) { 
    $row[$column] = htmlspecialchars($column); 
} 
+0

感謝您的信息是非常有益的。我不應該用特殊字符替換「&」嗎?我注意到帶有amperstands的字段仍然在拋出一個錯誤。 – Denoteone

+0

它逃脫了我:http://codepad.org/sfMR0JTn – SomeKittens

0

如何:

$row = mysql_fetch_assoc($resultID); 
$row = array_map('htmlentities', $row); 
$xml_output .= "\t\t<marker name='.$row['CompanyName']."' address='".$row['Address_1']."' phone='".$row['Phone']."' lat='".$row['Lat_Info']."' lng='".$row['Long_Info']."' county='".$row['County']."'/>\n";