如何使用 php 将 mysql 数据导出到 xml

How can export mysql data in to xml using php

以下代码用于将数据从 mysql table 导出为 xml 文件。我尝试了几个代码但没有得到结果。请检查并帮助我。

目前得到的结果是

8sarathsarathernakulam423432washington9rahulrahulernakulam21212121newyork10aaaa3london11bbbb1newyork12cccc2washington13dddd3london

代码

<?php
require_once "classes/dbconnection-class.php";
if(isset($_POST['export'])){
    header('Content-type: text/xml');
    $xml          = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    $root_element = "addressbook"; //fruits
    $xml         .= "<$root_element>";
    $query        = "SELECT AB.id, AB.name, AB.firstname, AB.street, AB.zipcode, AB.city_id, CI.city FROM address_book AS AB INNER JOIN city AS CI ON AB.city_id = CI.id";
    $result      = $mysqli->query($query);
    if (!$result) {
        die('Invalid query: ' . $mysqli->error());
    }

    while($result_array = $result->fetch_assoc()){
        $xml .= "<address>";
        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.="</address>";
    }
    $xml .= "</$root_element>";
    header ("Content-Type:text/xml");
    //header('Content-Disposition: attachment; filename="downloaded.xml"');
    echo $xml;
}
?>

浏览器显示

<?xml version="1.0" encoding="UTF-8"?><addressbook><address><id><![CDATA[8]]></id><name><![CDATA[sarath]]></name><firstname><![CDATA[sarath]]></firstname><street><![CDATA[ernakulam]]></street><zipcode><![CDATA[42343]]></zipcode><city_id><![CDATA[2]]></city_id><city><![CDATA[washington]]></city></address><address><id><![CDATA[9]]></id><name><![CDATA[rahul]]></name><firstname><![CDATA[rahul]]></firstname><street><![CDATA[ernakulam]]></street><zipcode><![CDATA[2121212]]></zipcode><city_id><![CDATA[1]]></city_id><city><![CDATA[newyork]]></city></address><address><id><![CDATA[10]]></id><name><![CDATA[a]]></name><firstname><![CDATA[a]]></firstname><street><![CDATA[a]]></street><zipcode><![CDATA[a]]></zipcode><city_id><![CDATA[3]]></city_id><city><![CDATA[london]]></city></address><address><id><![CDATA[11]]></id><name><![CDATA[b]]></name><firstname><![CDATA[b]]></firstname><street><![CDATA[b]]></street><zipcode><![CDATA[b]]></zipcode><city_id><![CDATA[1]]></city_id><city><![CDATA[newyork]]></city></address><address><id><![CDATA[12]]></id><name><![CDATA[c]]></name><firstname><![CDATA[c]]></firstname><street><![CDATA[c]]></street><zipcode><![CDATA[c]]></zipcode><city_id><![CDATA[2]]></city_id><city><![CDATA[washington]]></city></address><address><id><![CDATA[13]]></id><name><![CDATA[d]]></name><firstname><![CDATA[d]]></firstname><street><![CDATA[d]]></street><zipcode><![CDATA[d]]></zipcode><city_id><![CDATA[3]]></city_id><city><![CDATA[london]]></city></address></addressbook>

替换

$xml .= "<![CDATA[$value]]>";

$xml .= $value;

如果你想让它格式化 "nicely" 在浏览器中添加:

echo "<pre>";

之前:

echo $xml;

请注意这会破坏 XML 文件,但它在浏览器中看起来不错....如果这就是你想要的...

当我们处理 XML 和 HTML 时,最好的处理方式是 ever 通过解析器。 在这种特殊情况下,使用解析器进行操作可以保证有效 XML 和干净、简短的代码。

定义mySQL查询后,我们用版本和编码初始化一个新的DOMDocument,然后我们将他的->formatOutput设置为True,以缩进格式打印出XML :

$query = "SELECT AB.id, AB.name, AB.firstname, AB.street, AB.zipcode, AB.city_id, CI.city FROM address_book AS AB INNER JOIN city AS CI ON AB.city_id = CI.id";

$dom   = new DOMDocument( '1.0', 'utf-8' );
$dom   ->formatOutput = True;

然后,我们创建根节点并将其附加到 DOMDocument:

$root  = $dom->createElement( 'addressbook' );
$dom   ->appendChild( $root );

此时,在执行 mySQL 查询后,我们对每个结果行执行 while 循环;对于每一行,我们创建一个空节点 <address>,然后我们对每一行的字段执行 foreach 循环。对于每个字段,我们创建一个空的子节点,标签作为字段键,然后我们将字段值作为 CDATA 附加到子节点,并将相同的子节点附加到 <address> 节点;在每个 while 循环结束时,每个 <address> 节点附加到根节点:

$result     = $mysqli->query( $query );
while( $row = $result->fetch_assoc() )
{
    $node = $dom->createElement( 'address' );
    foreach( $row as $key => $val )
    {
        $child = $dom->createElement( $key );
        $child ->appendChild( $dom->createCDATASection( $val) );
        $node  ->appendChild( $child );
    }
    $root->appendChild( $node );
}

现在,您的 XML 已准备就绪。

如果您想将其保存到文件中,您可以通过以下方式完成:

$dom->save( '/Your/File/Path.xml' );

否则,如果您希望将其发送为XML,则必须使用此代码:

header( 'Content-type: text/xml' );
echo $dom->saveXML();
exit;

如果你想 在 HTML 页面中输出它, 你可以写这个代码:

echo '<pre>';
echo htmlentities( $dom->saveXML() );
echo '</pre>';

我建议使用 SimpleXMLElement 等库来创建 XML 文档。

$xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"UTF-8\" ?><{$root_element}></{$root_element}>"); 
while($result_array = $result->fetch_assoc()){
    foreach($result_array as $key => $value)
    {
        $address = $xml->addChild("address");
        //embed the SQL data in a CDATA element to avoid XML entity issues
        $addressFields = $address->addChild('"' . $key . '"', "<![CDATA[$value]]>");

       //No need to close the element
    }
}
Header('Content-type: text/xml');
print($xml->asXML());

转到您的 phpmyadmin 数据库导出并 select xml 文件格式。