【发布时间】:2016-05-28 00:37:32
【问题描述】:
以下代码用于将 mysql 表中的数据导出为 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 时,浏览器无法将其可视化。考虑查看浏览器显示的文档的来源,或将输出转储到文件中并查看。
-
@arkascha,谢谢,我已经用我的问题更新了浏览器结果。我检查了,但我没有发现问题。我不知道为什么结果不正确。
-
好的,看起来好多了!现在,如果您还告诉我们您得到的结果到底有什么问题,我们实际上可以帮助您解决问题...
-
我不明白什么不起作用
-
您的浏览器永远不会向您显示像
<?xml version="1.0" encoding="UTF-8"?><addressbook><address>...这样的xml,它只会显示标签内的值,但是如果您查看页面的源代码,您将看到您生成的完整xml
标签: php php-5.3 php-5.5 php-5.6