0

I am trying to get data from my database and store it in an xml file and everytime I run the code i get this error on line 2 at column 1: Document is empty.

 <?php 
    header ("Content-Type:text/xml");//Tell browser to expect xml
    include ("db_connect.php");
    $query = "SELECT * FROM winery"; 
    $result = mysqli_query($connection, $query) or die ("Error in query: $query. ".mysql_error()); 
    //Top of xml file
    $_xml = '<?xml version="1.0"?>'; 
    $_xml .="<winerys>"; 
    while($row = mysqli_fetch_array($result)) { 
    $_xml .="<winery>"; 
    $_xml .="<winery_id>".$row['winery_id']."</winery_id>"; 
    $_xml .="<winery_name>".$row['winery_name']."</winery_name>"; 
    $_xml .="<region_id>".$row['region_id']."</region_id>"; 
    $_xml .="</winery>"; 
    } 
    $_xml .="</winerys>"; 
    //Parse and create an xml object using the string
    $xmlobj=new SimpleXMLElement($_xml);
    //And output
    print $xmlobj->asXML();
    //or we could write to a file
    $xmlobj->asXML(winerys.xml);
    ?>
percy3872
  • 91
  • 1
  • 8
  • Which program does give this error message? It's not written in your question, but it's always good to know which thing actually gives the error message. And also, could you please tell what you expected instead of an empty document? – hakre Oct 31 '15 at 16:06
  • For the topic you ask about, please see as well [How to generate XML file dynamically using PHP?](http://stackoverflow.com/q/486757/367456) – hakre Oct 31 '15 at 16:09

1 Answers1

0

Assuming that you are getting a recordset the following ought to work, though it is untested. It uses a standard DOMDocument approach to generating the xml rather than building as a string and using SimpleXMLElement. It may not necessarily be as true as with javascript, but string concatenation can be slow at times, especially if the string grows quite large because of a high number of records. Also, one thing to note with your original code is the filename that you use to save the xml data into - it appears as an unquoted string so is probably being interpreted, incorrectly, as a constant.

 <?php 
    error_reporting( E_ALL );/* to aid debugging */
    include ("db_connect.php");

    $query = "select * from `winery`;"; 
    $result = mysqli_query( $connection, $query ) or die ('Sorry, there was an error'); 
    /* Create the DOM object */
    $dom=new DOMDocument('1.0','utf-8');
    /* Add the root node */
    $root=$dom->createElement('winerys');
    $dom->appendChild( $root );

    while( $row = mysqli_fetch_array( $result ) ) {
        /* Add a new item / child */
        $child=$root->appendChild( $dom->createElement('winery') );
        $child->appendChild( $dom->createElement( 'winery_id', $rs->winery_id ) );

        /* Use a CDATA section to allow for odd chars */
        $name=$dom->createCDATASection( $rs->winery_name );
        $winery=$dom->createElement( 'winery_name' );
        $winery->appendChild( $name );
        $child->appendChild( $winery );

        $child->appendChild( $dom->createElement( 'region_id', $rs->region_id ) );
    }

    /* save and echo */
    $xml = $dom->saveXML();
    $file = $dom->save('winerys.xml');

    header("Content-Type: text/xml");
    print_r( $xml );

?>

Created a test db table and this appears to work as expected.

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • It does, until you have an `&` in the content like a winery named `SomeName & Son`. I suggest creating a text node and appending it. The second argument of `DOMDocument::createElement()` is broken. `SimpleXMLElement::addChild()` has the same problem. – ThW Oct 30 '15 at 20:48