0

I have table data with headings: Keyword,Search Times, Last Search,Delete and I am providing a link to download the table data in XML format .

My code is:

    public function download_xml()
  {
//database configuration
$config['mysql_host'] = "localhost";
$config['mysql_user'] = "user";
$config['mysql_pass'] = "password";
$config['db_name']    = "db_name";
$config['table_name'] = "tablename";

 //connect to host
mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
//select database
@mysql_select_db($config['db_name']) or die( "Unable to select database");
$xml          = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$root_element = $config['table_name']."s"; //fruits
$xml         .= "<$root_element>";

//select all items in table
$sql = "SELECT * FROM ".$config['table_name'];
$result = mysql_query($sql);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

if(mysql_num_rows($result)>0)
{
   while($result_array = mysql_fetch_assoc($result))
   {
      $xml .= "<".$config['table_name'].">";

      //loop through each key,value pair in row
      foreach($result_array as $key => $value)
      {  
      //$key holds the table column name
         $xml .= "<".$key.">";
             //embed the SQL data in a CDATA element to avoid XML entity issues
         $xml .= "<![CDATA[".$value."]]>";

         //and close the element
         $xml .= "</".$key.">";
      }

      $xml.="</".$config['table_name'].">";
   }
}
//close the root element
$xml .= "</$root_element>";

//send the xml header to the browser
//header ("Content-Type:text/xml");
 header ("Content-Type: text/xml; charset=latin1"); 
//output the XML data
echo $xml;

  }

Linking is done by providing a link at view:

<?php echo $this->Html->link('Export to XML ',array('controller'=>'admin','action'=>'download_xml'), array('target'=>'_self'));
?>  

It's giving me error:

    XML Parsing Error: junk after document element
    Location: http://www.abc.co.uk/admin/download_xml
    Line Number 1, Column 173285:
<?xml version="1.0" encoding="UTF-8"?><search_logss><search_logs><id><![CDATA[27]]></id><keyword><![CDATA[LRG Core Collection]]></keyword><user_id><![CDATA[0]]></user_id><priority><![CDATA[30]]></priority><created><![CDATA[2014-11-28 06:57:59]]></created><modified><![CDATA[2015-04-05 09:13:21]]></modified></search_logs></search_logss><!DOCTYPE html>
------------------------------------------------------------------------------------------------------------------------------------------^

Is anyone having idea why is it so? Thanx in advance

hakre
  • 193,403
  • 52
  • 435
  • 836
  • look at http://stackoverflow.com/questions/15762857/xml-parsing-error-junk-after-document-element and http://stackoverflow.com/questions/26008046/xml-parsing-error-junk-after-document-element-in-twilio.. are you echoing something? or blank spaces are coming in betweent he xml tags... – Nishant Solanki Apr 16 '15 at 05:32
  • this is my entire code and I have no clue as I m not able to spot the error – Komal Fartiyal Apr 16 '15 at 05:36
  • check is there blank spaces coming in your code or what... – Nishant Solanki Apr 16 '15 at 05:40
  • No space is coming as it's showing : id =>27 keyword =>LRG Core Collection user_id =>0 priority =>30 created =>2014-11-28 06:57:59 modified =>2015-04-05 09:13:21 – Komal Fartiyal Apr 16 '15 at 05:56
  • than please provide your whole code.. so it can help understand more – Nishant Solanki Apr 16 '15 at 05:59
  • @NishantSolanki What I have provided you is the whole code,only db connections code have been removed – Komal Fartiyal Apr 16 '15 at 06:08
  • You could try `trim()`-ing the values of `$key` and `$value` – mrun Apr 16 '15 at 06:14
  • Could you please share which part of the error message is unclear and/or unexpected for you? Have you actually verified if there is junk after the document element and have you taken a look how that junk looks like? That normally answers such questions right away. Please update your question. – hakre Apr 16 '15 at 06:14
  • @hakre Please view the updated question – Komal Fartiyal Apr 16 '15 at 06:16
  • @NishantSolanki I have edited n include my whole code plz have a view – Komal Fartiyal Apr 16 '15 at 06:16
  • @Komal Fartiyal: Can't see anything addressing my clarification questions. In that light closed as a duplicated as too localized like the other one. It's not clear why you don't expect the error there. You also didn't share the XML at all you ask about here. Please reduce your example to the bare minimum to show the issue. Also you're doing the mistake to build the XML "by hand" (concatenating strings). This is error prone, a standard suggestion is to use an XML library (that's just FYI not Q&A related). – hakre Apr 16 '15 at 06:19
  • Is it clear to you what the term *document element* means? What is your understanding of *junk* that is spoken about in the error message? Can you provide that *junk* with your question? If not, why not? – hakre Apr 16 '15 at 06:20
  • Yes,it's clear to me.Could you suggest me XML library link – Komal Fartiyal Apr 16 '15 at 06:21
  • @KomalFartiyal I have used your code for generating the xml and its working fine.. unfortunately your question is marked as duplicate, so i will not be able to post the code in answer – Nishant Solanki Apr 16 '15 at 06:23
  • I suggest you first check how that junk looks like. It often shows the cause of the problem, so you have something to take with you. – hakre Apr 16 '15 at 06:23
  • 1
    @NishantSolanki: The XML creation as outline fails depending on the data used. It can work, but it's not stable. --- There is diverse related Q&A material on site, I suggest a search for on how to create XML based on a database result like (no preference, get more than just one suggestion) [Get MySQL database output via PHP to XML](http://stackoverflow.com/q/5112282/367456). – hakre Apr 16 '15 at 06:25
  • @hakre ohh ok.. may be the junk data coming from her database.. it can be the possible issue... – Nishant Solanki Apr 16 '15 at 06:27
  • @KomalFartiyal look at `simpleXML` library.. here is some reference.. http://stackoverflow.com/questions/486757/how-to-generate-xml-file-dynamically-using-php – Nishant Solanki Apr 16 '15 at 06:34

0 Answers0