创建xml文档

时间:2011-02-28 13:28:06

标签: php mysql xml

我的数据库(Mysql)中有一个包含6列的表。我使用PHP表单将数据存储在此数据库中。其中一列是网址。我能够将数据存储在数据库中,现在我想构建一个以URL作为数据的XML文档。

Database name: probe_config
table name   : webmeasurementsuite

我的数据库详情如下:

url:(varchar,binary and the default value is null)
replication:(integer,unsigned,zerofill and default value is null)
wait:(integer,unsigned,zerofill and default value is null)
timeout:(integer,unsigned,zerofill and default value is null)
clearcache:(varchar,binary and default value is null)
name:(varchar,binary and default value is null)
id  :(integer,notnull,autoinc,unsigned,zerofill and default value is null)

有人可以帮我转换为xml文档吗?

1 个答案:

答案 0 :(得分:0)

将数据库结果作为数组获取后,可以使用函数like this将该数组转换为XML文档。但是,不要认为这样的功能是最好的性能或软件设计。

假设您的数据库连接已经完成,

$sth = $dbh->prepare("SELECT url FROM webmeasurementsuite");
$sth->execute();
$result = $sth->fetchAll();

$xml = '<?xml version="1.0" encoding="UTF-8"?>';

foreach ($result as $v) {
    $xml .= '<url>'.htmlspecialchars($v['url'], ENT_COMPAT, 'UTF-8').'</url>';
}