2010-06-16 94 views
0

需要帮助以下内容:如何使用Javascript或PhP替换XML中的无效字符

运行PhP,javascript,MySQL,XML。

1)从MySQL中检索文件并将其存储到XML文件中。
2)使用javascript函数加载XML文件(存储这些数据)。
3)它在XML文件中产生无效字符。

步骤1:在PHP代码的样本 - >装载MySQL数据库到数据存储到XML文件

$file= fopen("MapDeals2.xml", "w"); 
$_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"; 
$_xml .="<MAP>\n"; 

while($row1_ThisWeek = mysql_fetch_array($result1_ThisWeek)) { 
    $rRName = $row1_ThisWeek['Retailer_Name']; 
    $rRAddress = $row1_ThisWeek['Retailer_Address1']; 
    $rRAddressPostCode = $row1_ThisWeek['Retailer_AddressPostCode1']; 

//} commented out from the original 


    $_xml .= "<DEAL>\n"; 
    $_xml .= "<DealDescription>" . $d_Description . "</DealDescription>\n"; 
    $_xml .= "<DealURL>" . $d_URL . "</DealURL>\n"; 
    $_xml .= "<DealRName>" . $rRName . "</DealRName>\n"; 
    $_xml .= "<DealRAddress>" . $rRAddress . "</DealRAddress>\n"; 
    $_xml .= "<DealRPostCode>" . $rRAddressPostCode . "</DealRPostCode>\n"; 
    $_xml .= "</DEAL>\n"; 

} 


//} commented out from the original 
$_xml .="</MAP>\n"; 
fwrite($file, $_xml); 
fclose($file); 

。步骤2:在Javscript代码的样本 - >装载XML文件

xhttp.open("GET","Test2.xml", false); 
xhttp.send(""); 
xmlDoc=xhttp.responseXML; 

var x=xmlDoc.getElementsByTagName("Employee"); 

parser = new DOMParser(); 
xmlDoc = parser.parseFromString("MapDeals2.xml", "text/xml"); 


for (i=0;i<x.length;i++) 
{ 
// alert ('Generating FOR loop'); 
    var d1 = x[i].getElementsByTagName("EmployeeDescription")[0].childNodes[0].nodeValue; 
    var e1 = "<br></br>"; 
. 
. 
. 


} 

有没有解决上述问题的方法?希望能早日得到你的回复。

干杯

+0

你的代码乱七八糟。在试图清理它的时候,它还有两个比开启支架更近的支撑杆。我评论他们。如果此修改与您的代码不符,请编辑您的问题。 – Artefacto 2010-06-16 12:51:31

回答

0

这可能是一个字符集问题。你的MySQL连接具有编码iso-8859-1或类似的东西,而XML预计是UTF8。你必须把它转换到某个地方。

相关问题