0

I have a variable with XML and I want to make it download using a download button and I can't find a way to do that. Below is the last part of the code. I want this "newxml" variable to download as a XML file. How can I do it?

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

I tried to make it downloadable like below and it makes and error. can someone please make this correct

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

Here is the error message

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>
Jagath01234
  • 139
  • 1
  • 10
  • see this topic : http://stackoverflow.com/questions/3552755/how-to-save-xml-using-php – SayJeyHi Aug 03 '15 at 17:25
  • and after saving your file create a link with `download="$link"` attribute . – SayJeyHi Aug 03 '15 at 17:26
  • Here my problem is that I can't make this as a HTML 5 file. an error jumps out when I put and the other tags like and . – Jagath01234 Aug 03 '15 at 17:44
  • I think your question is not that clear. I found http://stackoverflow.com/q/4412395/367456 but I think it's only part of your issue. – hakre Aug 05 '15 at 22:10

1 Answers1

0

I finally got the answer.

This is because we can't put html inside a XML file. So what I have to do is create another file and give this files name as a download link. This files name is "a.php".Therefore the new download files code is

<!DOCTYPE html>
<html>

<a href="a.php" download="sample.xml">Download XML</a>
</html>
Jagath01234
  • 139
  • 1
  • 10
  • There normally is no problem to put HTML inside an XML file. All you need to do is to encode it properly as XML. Normally just the correct encoding for reserved XML characters and/or CData sections are used for that. – hakre Aug 05 '15 at 22:07