在线等。要用Ci框架写个API 生成xml文件
要用Ci框架写个API 生成xml文件,不知道有没有人写过,共享下。怎么定义一些key 等等 生成XML文件很简单的。
就是有标签对的文件吧。
你是想做什么。从数据库里面调用最新的数据那种XML数据文件吗?
如果是的话我可以单写个函数来。是不是要这个功能呢? 你是不是从数据库读取一些东西啊???
到底是不是在线等的。
<tb_name>
<col_1>val_1</col_1>
<col_2>val_1</col_2>
</tb_name>
这样子算是一行是这样吗? 这样可以吗?
<?php
//set your dabase info here
$conn=mysql_connect("localhost","root","1");
mysql_select_db('a0611200215');
//set your table name here
$tb_name="users";
//get the columns array of the table
$sql="SELECT COLUMN_NAME
FROM information_schema.COLUMNS
WHERE TABLE_NAME='$tb_name'
";
$res=mysql_query($sql);
$col_arr=array();
while($row=mysql_fetch_array($res))
{
$col_arr[]=$row['COLUMN_NAME'];
}
$xml_str='<'.$tb_name.'>';
//you can limit row number or scend
$sql="select * from $tb_name
limit 5";
$res=mysql_query($sql);
while($row=mysql_fetch_assoc($res))
{
foreach($row as $key=>$val)
{
$xml_str.='<'.$key.'>';
$xml_str.=$val;
$xml_str.='</'.$key.'>';
}
}
$xml_str.='</'.$tb_name.'>';
//open a file and write
$filename = 'test.xml';
$somecontent =$xml_str;
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'w')) {
echo "can't open $filename";
exit;
}
if (fwrite($handle, $somecontent) === FALSE) {
echo "can't wite into$filename";
exit;
}
echo "success";
fclose($handle);
}
else
{
echo "can't write";
}
?> 谢谢 成功生成XML php有个simpleXMl的,查手册。
这个跟ci没有关系
前面的那位朋友使用的是字符串拼接的方式,不够规范。
应该使用dom的方式生成节点和属性。 回复 6# xiaoniao
那是什么呀?DOM是网页元素的意思吗?能具体说说是什么东西吗?有什么优点?
页:
[1]