Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export from MySQL to a XML using

Status
Not open for further replies.

JamesFlowers

Programmer
Mar 23, 2001
97
GB
Hi , I am trying to export from MySQL to a XML using code below , and it returns

"Warning: SimpleXMLElement::addChild() [simplexmlelement.addchild]: unterminated entity reference on line 20"

any ideas please?

TIA


Code:
<?php
 
    //Create Database connection
    $db = mysql_connect('XXXXXXXXXX','XXXXXXXXXX','XXXXXXXXXX');
    if (!$db) {
        die('Could not connect to db: ' . mysql_error());
    }
 
    //Select the Database
    mysql_select_db('XXXXXXXXXX') or  die (mysql_error());
 
    $result = mysql_query("select CustomerName  from Customers", $db);  
 
    //Create SimpleXMLElement object
    $xml = new SimpleXMLElement('<xml/>');
 
    //Add each column value a node of the XML object
    while($row = mysql_fetch_assoc($result)) {
        $mydata = $xml->addChild('mydata');
        $mydata->addChild('Name',$row['CustomerName']);
    }
 
    mysql_close($db);
    //Create the XML file
    $fp = fopen("employeeData.xml","wb");
 
    //Write the XML nodes
    fwrite($fp,$xml->asXML());
 
    //Close the database connection
    fclose($fp);
 
?>

James Flowers
Crystal Consultant
 
Hi

See Volker Grabsch's comment on the [tt]SimpleXMLElement::addChild()[/tt] manual page.

Then let us try to reproduce it :
Code:
[blue]php >[/blue] $xml = new SimpleXMLElement('<xml/>');

[blue]php >[/blue] $xml->addChild('name', 'foo & bar');
PHP Warning:  SimpleXMLElement::addChild(): unterminated entity reference             bar in php shell code on line 1
PHP Stack trace:
PHP   1. {main}() php shell code:0
PHP   2. SimpleXMLElement->addChild() php shell code:1

[blue]php >[/blue] $xml->addChild('name', 'foo &amp; bar');

[blue]php >[/blue]
Yepp, that seems to be your problem.


Feherke.
feherke.ga
 
Hi

Please note that the 2nd [tt]addChild()[/tt]'s 2nd parameter is actually 'foo [highlight]&amp;[/highlight] bar'.

( Sadly the forum engine transforms SGML character entities when posted inside [tt]
Code:
[/tt] tags. )

Feherke.
[url=http://feherke.ga/]feherke.ga[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top