dongshanji3102 2014-03-24 12:24
浏览 42
已采纳

使用JQuery AJAX将XML写入文件

I have to write to an XML file and since the only way I know to do that from JavaScript is using AJAX that's the way I'm trying to achieve so but it's been a while since I last used it and I'm not being able to parse the text so I get HTML codes for characters instead of the characters themselves.

What I have:

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 file

<?php

$fileName = htmlspecialchars($_POST["filename"]);
$content = htmlspecialchars($_POST["content"]);
$file = fopen($fileName, "w");
fwrite($file, $content);
fclose($file);
?>

What I am trying to achieve:

<?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>

What I get:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;SeriesList&gt;    
&lt;Series&gt;
 &lt;title&gt;Ookami to Koushinryou&lt;/title&gt;
 &lt;score&gt;6&lt;/score&gt;
 &lt;episodes&gt;7&lt;/episodes&gt;
 &lt;episodesTotal&gt;23&lt;/episodesTotal&gt;
 &lt;episodeLength&gt;20&lt;/episodeLength&gt;
 &lt;timesWatched&gt;dropped&lt;/timesWatched&gt;
&lt;/Series&gt;    
&lt;Series&gt;
 &lt;title&gt;Speed Grapher&lt;/title&gt;
 &lt;score&gt;5&lt;/score&gt;
 &lt;episodes&gt;7&lt;/episodes&gt;
 &lt;episodesTotal&gt;24&lt;/episodesTotal&gt;
 &lt;episodeLength&gt;20&lt;/episodeLength&gt;
 &lt;timesWatched&gt;dropped&lt;/timesWatched&gt;
&lt;/Series&gt;    
&lt;/SeriesList&gt;

So what can I do? I tried utf8_encode() but it did not work and I'm not exactly what you'd call versatile with PHP so I'm out of ideas.

I searched the net for everything I thought of but nothing worked so far.

Thanks in advance for everything.

  • 写回答

1条回答 默认 最新

  • douhuan7862 2014-03-24 12:28
    关注

    Remove all your uses of htmlspecialchars.

    Your file system doesn't use HTML to express filenames, so you shouldn't convert your filenames to HTML. (You need to sanitise your filename though, at the moment you are letting people overwrite just about any file they like on your server!)

    Your XML data is already XML, so you couldn't convert it to an HTML representation of XML.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测