我有一个XML变量,我想让它使用下载按钮下载,我找不到一种方法来做到这一点。下面是代码的最后一部分。我希望这个"newxml"变量作为XML文件下载。我该怎么做呢?
<?php
require_once('b.php');
$json = file_get_contents('sample.json');
$php_array = json_decode($json, true);
header("Content-type: text/xml");
$xml = Array2XML::createXML('root-element-here', $php_array);
$newxml=$xml->saveXML();
echo $newxml;
$file = 'XML.xml';
file_put_contents($file, $newxml);
?>
我试着让它像下面下载,它犯了一个错误。谁能把这个更正一下
<!DOCTYPE html>
<html>
<body>
<?php
require_once('b.php');
$json = file_get_contents('sample.json');
$php_array = json_decode($json, true);
header("Content-type: text/xml");
$xml = Array2XML::createXML('root-element-here', $php_array);
$newxml=$xml->saveXML();
$file = 'XML.xml';
file_put_contents($file, $newxml);
?>
<a href="XML.xml" download="sample.xml">Download XML</a>
</body>
</html>
下面是错误信息
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<html>
<body>
<a href="XML.xml" download="sample.xml">Download XML</a>
</body>
</html>
我终于得到了答案。
这是因为我们不能把html放在XML文件中。我要做的是创建另一个文件并给这个文件名作为下载链接。这个文件名是"a.php"
.因此新的下载文件代码是
<!DOCTYPE html>
<html>
<a href="a.php" download="sample.xml">Download XML</a>
</html>