使用php从mysql db创建xml文件 [英] using php to create an xml file from a mysql db

查看:75
本文介绍了使用php从mysql db创建xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用php创建xml文件.每次我运行代码时,页面都会从某一点以文本形式在屏幕上显示代码.我的代码如下:

im trying to create a xml file using php.everytime i run the code the page displayes the code from a certain point as text on the screen.the code i hav is as follows:

<?php
    if(!$dbconnet = mysql_connect('I took out the details')){
        echo "connection failed to the host.";
        exit;
    }
    if (!mysql_select_db('siamsati_db')){
        echo "Cannot connect to the database.";
        exit;
    }
    $table_id = 'events';
    $query = "SELECT * FROM $table_id";
    $dbresult = mysql_query($query, $dbconnect);

    $doc = new DomDocument('1.0');

    $root = $doc->createElement('root');
    $root = $doc->appendChild($root);

    while($row = mysql_fetch_assoc($dbresult)){
        $ooc = $doc->createElement($table_id);
        $occ = $root->appendChild($occ);

        foreach ( $row as $fieldname => $fieldvalue){

            $child = $doc->createElement($fieldname);
            $child = $occ->appendchild($child);

            $value = $doc->createTextNode($fieldvalue);
            $value = $child->appendChild($value);
        }
    }

    $xml_string = $doc->saveXML();
    echo $xml_string;

?>

,显示的页面显示:

createElement('root'); $ root = $ doc-> appendChild($ root); while($ row = mysql_fetch_assoc($ dbresult)){$ ooc = $ doc-> createElement($ table_id); $ occ = $ root-> appendChild($ occ); foreach( $ row as $ fieldname => $ fieldvalue){ $ child = $ doc-> createElement($ fieldname); $ child = $ occ-> appendchild($ child); $值= $ doc-> createTextNode($ fieldvalue); $ value = $ child-> appendChild($ value); $ xml_string = $ doc-> saveXML(); echo $ xml_string; ?>

createElement('root'); $root = $doc->appendChild($root); while($row = mysql_fetch_assoc($dbresult)){ $ooc = $doc->createElement($table_id); $occ = $root->appendChild($occ); foreach ( $row as $fieldname => $fieldvalue){ $child = $doc->createElement($fieldname); $child = $occ->appendchild($child); $value = $doc->createTextNode($fieldvalue); $value = $child->appendChild($value); } } $xml_string = $doc->saveXML(); echo $xml_string; ?>

是否有我遗漏的东西.我首先检查了所有引述,认为它们是对的,但它们似乎都是正确的.任何对我做错事的建议都值得赞赏?

is there something ive missed.ive checked all the quotes thinking it was that at first but they all seem to be right.any suggestions on what im doing wrong are much appreciated?

推荐答案

将内容类型设置为XML,以便浏览器将其识别为XML.

Set the content type to be XML, so that the browser will recognise it as XML.

header( "content-type: application/xml; charset=ISO-8859-15" );

在您的代码中将其更改为:

In your code Change it to:

// Set the content type to be XML, so that the browser will   recognise it as XML.
header( "content-type: application/xml; charset=ISO-8859-15" );

// "Create" the document.
$doc = new DOMDocument( "1.0", "ISO-8859-15" );

+++我认为您可以做类似的事情

+++I think you can do something like this

<root>
<?
   foreach ( $row as $fieldname => $fieldvalue){
    ?>
    <events>
        <fieldname><?=fieldname; ?></fieldname>
        <fieldvalue><?=$fieldvalue; ?></fieldvalue>
    </events>
    }
?>
</root>

这篇关于使用php从mysql db创建xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆