使用PHP DOM从MySQL数据创建XML文件 [英] Using PHP DOM to create XML files from MySQL data
本文介绍了使用PHP DOM从MySQL数据创建XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
您将不得不从mysql数据创建dom xml,然后将其保存在xml文件中。例如: p>
$ sql ='select * from messages';
$ run = mysql_query($ sql,$ link);
if($ run&& mysql_num_rows($ run)){
$ doc = new DOMDocument('1.0');
$ doc-> formatOutput = true;
$ doc-> preserveWhiteSpace = true;
$ root = $ doc-> createElement('data');
$ doc-> appendChild($ root);
while(($ fetch = mysql_fetch_assoc($ run))!== false){
$ node = $ doc-> createElement('node');
$ root-> appendChild($ node);
foreach($ fetch as $ key => $ value){
createNodes($ key,$ value,$ doc,$ node);
}
}
$ doc-> save(thexmlfile.xml);
}
函数createNodes($ key,$ value,$ doc,$ node){
$ key = $ doc-> createElement($ key);
$ node-> appendChild($ key);
$ key-> appendChild($ doc-> createTextNode($ value));
}
现在,您应该会看到xml文件。
希望,它有帮助。
I have created a MySQL table and would like to save the contents of the table in an XML file for use by other applications. I can access the data fine and echo the data on a broswer window, but on saving using the DomDocument::save('thexmlfile.xml'), I cannot see any new files created in the directory location of the running PHP file.
解决方案
you will have to create the dom xml from the mysql data and then save it in an xml file.An example:
$sql = 'select * from messages';
$run = mysql_query($sql, $link);
if( $run && mysql_num_rows( $run ) ) {
$doc = new DOMDocument( '1.0' );
$doc->formatOutput = true;
$doc->preserveWhiteSpace = true;
$root = $doc->createElement( 'data' );
$doc->appendChild( $root );
while( ( $fetch = mysql_fetch_assoc( $run ) )!== false ) {
$node = $doc->createElement( 'node' );
$root->appendChild( $node );
foreach( $fetch as $key => $value ) {
createNodes( $key, $value, $doc, $node );
}
}
$doc->save("thexmlfile.xml");
}
function createNodes( $key, $value, $doc, $node ) {
$key = $doc->createElement( $key );
$node->appendChild( $key );
$key->appendChild( $doc->createTextNode( $value ) );
}
Now, you should see the xml file.
Hope, it helps.
这篇关于使用PHP DOM从MySQL数据创建XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文