开发者

How to replace invalid characters in XML using Javascript or PhP

开发者 https://www.devze.com 2023-01-03 20:51 出处:网络
Need help here for the following: Running PhP, javascript, MySQL, XML. 1) Retrieving file from MySQL and stored it onto XML file.

Need help here for the following:

Running PhP, javascript, MySQL, XML.

1) Retrieving file from MySQL and stored it onto XML file.

2) Use javascript function to load XML file (that stored those data).

3) It produces invalid characters in XML file.

STEP 1 : Sample of the code in PhP -> Loading MySQL DB to store data onto XML file

$file开发者_C百科= 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);

STEP 2 : Sample of the code in Javscript -> Loading XML file

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>"; 
.
.
.


}

Is there a solution for the above? Looking forward to hear from you soon.

Cheers


It probably is a charset problem. Your MySQL connection has encoding iso-8859-1 or something like that, while the XML is expected to be in UTF8. You have to convert it somewhere.

0

精彩评论

暂无评论...
验证码 换一张
取 消