0
我必須寫入XML文件,因爲我知道從JavaScript執行此操作的唯一方法是使用AJAX,這是我嘗試實現的方式,但是從我開始最後使用它,我無法解析文本,所以我得到的是字符的HTML代碼,而不是字符本身。使用JQuery將XML寫入文件AJAX
我有什麼:
AJAX
$.ajax({
type: 'POST',
url: "writeXML.php",
dataType: 'xml',
data: {filename: "test.xml", content: listXML},
error: function() {
alert("Unknown error. Data could not be written to the file.");
},
success: function() {
window.open("test.xml");
}
});
PHP文件
<?php
$fileName = htmlspecialchars($_POST["filename"]);
$content = htmlspecialchars($_POST["content"]);
$file = fopen($fileName, "w");
fwrite($file, $content);
fclose($file);
?>
我想實現:
<?xml version="1.0" encoding="UTF-8"?>
<SeriesList>
<Series>
<title>Ookami to Koushinryou</title>
<score>6</score>
<episodes>7</episodes>
<episodesTotal>23</episodesTotal>
<episodeLength>20</episodeLength>
<timesWatched>dropped</timesWatched>
</Series>
<Series>
<title>Speed Grapher</title>
<score>5</score>
<episodes>7</episodes>
<episodesTotal>24</episodesTotal>
<episodeLength>20</episodeLength>
<timesWatched>dropped</timesWatched>
</Series>
</SeriesList>
我能得到什麼:
<?xml version="1.0" encoding="UTF-8"?>
<SeriesList>
<Series>
<title>Ookami to Koushinryou</title>
<score>6</score>
<episodes>7</episodes>
<episodesTotal>23</episodesTotal>
<episodeLength>20</episodeLength>
<timesWatched>dropped</timesWatched>
</Series>
<Series>
<title>Speed Grapher</title>
<score>5</score>
<episodes>7</episodes>
<episodesTotal>24</episodesTotal>
<episodeLength>20</episodeLength>
<timesWatched>dropped</timesWatched>
</Series>
</SeriesList>
那麼我該怎麼辦?我嘗試過utf8_encode(),但它不起作用,我不完全用PHP調用多功能,所以我沒有想法。
我在網上搜索了我想到的所有東西,但到目前爲止沒有任何工作。
在此先感謝您的一切。
這就是複製粘貼代碼時發生的情況。 非常感謝。 –