如何使用Javascript或PhP替换XML中的无效字符

时间:2010-06-16 12:38:33

标签: php javascript xml

此处需要以下帮助:

运行PhP,javascript,MySQL,XML。

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

第1步:PhP中的代码示例 - >加载MySQL DB以将数据存储到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中的代码示例 - &gt;加载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>"; 
.
.
.


}

上面有解决方案吗?期待很快收到你的回复。

干杯

1 个答案:

答案 0 :(得分:0)

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