Ⅰ php输出xml内容
PHP生成XML的方法很多,这里演示最基本,最简单的字符串构造法。就是使用字符串构造或者拼接成xml数据格式,然后输出或者生成xml文件。
<?php
$data=array(
array(
'title'=>'',
'country'=>'china',
'name'=>'网络',
),
array(
'title'=>'google',
'country'=>'usa',
'name'=>'谷歌',
)
);
//构造xml数据格式
$xml="<?xmlversion="1.0"encoding="utf-8"?> ";
$xml.="<data> ";
foreach($dataas$itm){
//循环构造xml单项
$item="<item> ";
$item.="<title>".$itm['title']."</title> ";
$item.="<country>".$itm['country']."</country> ";
$item.="<name>".$itm['name']."</name> ";
$item.="</item> ";
$xml.=$item;
}
$xml.="</data> ";
//输出xml数据
echo$xml;
?>
生成的数据格式如下:
Ⅱ 如何用PHP生成XML
$sql = "查询数据库文件";
$query = mysql_query($sql);
echo "<?xml version='1.0' encoding='utf-8' ?>";
echo "<photos>";
while(@$result = mysql_fetch_array($query)){
echo "<photo desc='$result[文件名字段]' url='_pics/$result[文件名字段]' />";
}
echo "</photos>";
--------------------------------------------------------
$this->_delImage('/_pics');
function _delImage($path){
if(is_dir($path)){
$dp=dir($path);
while($file=$dp->read())
if($file!='.'&&$file!='..'){
$this->_delImage($path.'/'.$file);
}
$dp->close();
}
echo "<photo desc='$path' url='$path' />";
}
可能有出入 自己看着修改
PS:我才昏呢 读取数据库比读文件夹方便好不好。
Ⅲ php生成 xml 的问题
其实一般你动态生成网站最新的5万条数据已经可以满足要求了,少一点说最新的5000也是ok的,不过你真的想这么做的话
<?php
$i=0;
$fp_r = fopen('slnew.txt','r');//只读模式打开txt文档,数据源就算是数据库也行,自己看着办
$fp_w = fopen('islnew'.$i.'.txt','w');//只写模式打开txt文档
$content = '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
$content = $content.'<urlset>'."\r\n";
while(!feof($fp_r)){
if($i%50000==0){
fclose($fp_w);
$fp_w = fopen('islnew'.($i/50000).'.txt','w');//只写模式打开txt文档,这里已经在新建文件了
}
$get=fgets($fp_r);//读取一行文字
$content = $content.'<url>'."\r\n";
$content = $content.'<loc>'.$get.'</loc>'."\r\n";
$content = $content.'</url>'."\r\n";
$i++;
}//输出整个文本内容
$content = $content.'</urlset>'."\r\n";
fwrite($fp_w,$content);//写入hello tocus!
fclose($fp_r);
fclose($fp_w);
?>
自己看着调整吧- -,我从以前写过的内容里加了计数,你可以按照自己需求调整下
Ⅳ php页面是gb2312编码,想生成unicode的xml文件,该如何编写php程序
如果你用的是php5的话,可能得看看
http://www.zypx.cn/technology/2010060619837242.html
php5和php6在unicode上的设定是有区别的,php6扩大了php5支持unicode的特性。
关于转码:
http://dodomail.javaeye.com/blog/195361
http://zhg.me/2009/07/php-unicode-transcoding-and-decoding-achieve.htm/comment-page-1?replytocom=472
转码时可能要安装iconv:
http://apps.hi..com/share/detail/18328380
php帮助网站:
http://cn2.php.net/
关于文件内容写入的例子:
http://cn2.php.net/fwrite
根据你的要求,如果要保存成unicode的xml文件的话可能麻烦点,看你的php是哪个版本的,有没有iconv库什么的。
<?php
//gb2312转换为unicode
function gb2un($g)//传入gb2312字符串返回unicode码
{
preg_match_all("/[\x80-\xff]?./",$g,$ar);
$str = "";
foreach($ar[0] as $v)
{
$str = $str."".utf8_unicode(iconv("gb2312","utf-8",$v)).";";
}
return $str;
}
$myString = gb2un("123我");
$fh=fopen('test.txt',"w");
fwrite($fh,unicode_encode($myString));
fclose($fh);
?>
Ⅳ 如何通过PHP生成和获取XML格式数据
1自己拼,XML编码
<?php
header('Content-type:text/xml');
echo "<?xml version='1.0' encoding='utf-8'>";
echo "<book>";
echo "<PHP>";
echo "<name>PHP程序开发范例宝典</name>";
echo "<price 单位='元/本'>89.00</price>";
echo "<date>2007-09-01</date>";
echo "</PHP>";
echo "</book>";
?>
拼接的效果
Ⅵ 如何在php文件里写xml
php文件里写xml方法:
1、Xml代码
<?php
$data_array=array(
array(
'title'=>'title1',
'content'=>'content1',
'pubdate'=>'2009-10-11',
),
array(
'title'=>'title2',
'content'=>'content2',
'pubdate'=>'2009-11-11',
)
);
//属性数组
$attribute_array=array(
'title'=>array(
'size'=>1
)
);
$string=<<<XML
<?xmlversion='1.0'encoding='utf-8'?>
<article>
</article>
XML;
$xml=simplexml_load_string($string);
foreach($data_arrayas$data){
$item=$xml->addChild('item');
if(is_array($data)){
foreach($dataas$key=>$row){
$node=$item->addChild($key,$row);
if(isset($attribute_array[$key])&&is_array($attribute_array[$key]))
{
foreach($attribute_array[$key]as$akey=>$aval){
//设置属性值
$node->addAttribute($akey,$aval);
}
}
}
}
}
echo$xml->asXML();
?>
Ⅶ php把xml转换为字符串
楼主我教你吧,首先xml文件里的内容为
<?xmlversion="1.0"encoding="ISO-8859-1"?>
<content
<name>lishi</name>
<age>17</age>
</content>
读取xml文件内容
$str=file_get_contents($xml);$xml为xml文件路径地址
将读取的字符串内容转化为xml对象
$obj=simplexml_load_string($str)
操作对象里的数据
$obj->name="lishi111";
$obj->age=77;
拼接新的字符串
$strNew="<?xml version='1.0' encoding='ISO-8859-1'?>";
$str.="<content><name>".$obj->name."</name";
$str.="<age>".$obj->age."</age></content>";
将新的字符串写入xml文件
file_put_content($xml,$strNew);
最后一步拿分来吧,哈哈哈哈。有问题继续。。。
Ⅷ php将XML转换成字符串!
$str = $xml->asXML();
$str就是你要的字符串
Ⅸ php 查询mysql生成xml
没啥难的,只是你当它是xml罢了,把它看到字符串,拼成xml格式,再那么一生成xml文件(或者直接用text/xml显示)即可。
Ⅹ 如何用php从数据库读取数据并生成xml文件
我的思路是,直接使用动态的xml,让flash读取这个文档,这样就不用实时的去生成xml文件了。当然,这个xml文件是.php格式的,所以你必须在flash中吧读取的文件地址改成php的,就跟你写一个php页面一样,不同的是这个php文件输出的内容是一个xml格式的文本。
比如你现在建立文件 xml.php
<?php
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<gallery>
<settings>";
//若此处也有动态信息 按需要进行调用
echo"</settings>
<items>";
//在此循环你的图片数据
$data = ??
while( $data ) {
echo "<item source=\"".$data['source']."\" description=\"".$data['description']."\" />";
}
echo '</items>';
?>