search
Homephp教程php手册php生成xml实例方法

本文章提供二种生成xml的方法,第一种是直接查询数据库,查询在php页面输出xml格式的数据,第二种方法是利用了php DOMDocument组件生成xml实例原理有一点不同.

php生成xml实例方法代码如下:

<?php
$sql = "查询数据库文件";
$query = mysql_query($sql);
echo "<?xml version=&#39;1.0&#39; encoding=&#39;utf-8&#39; ";
echo "<photos>";
while (@$result = mysql_fetch_array($query)) {
    echo "<photo desc=&#39;$result[文件名字段]&#39; url=&#39;_pics/$result[文件名字段]&#39; />";
}
echo "</photos>";
//--------------------------------------------------------
$this->_delimage(&#39;/_pics&#39;);
function _delimage($path) {
    if (is_dir($path)) {
        $dp = dir($path);
        while ($file = $dp->read()) if ($file != &#39;.&#39; && $file != &#39;..&#39;) {
            $this->_delimage($path . &#39;/&#39; . $file);
        }
        $dp->close();
    }
    echo "<photo desc=&#39;$path&#39; url=&#39;$path&#39; />";
}
//利用domdocument
$doc = new domdocument("1.0", "gb2312"); //声明文档类型
$doc->formatoutput = true; //设置可以输出操作
//声明根节点,最好一个xml文件有个跟节点
$root = $doc->createelement("root"); //创建节点对象实体
$root = $doc->appendchild($root); //把节点添加进来
// for($i=1;$i<100;$i++){  //循环生成节点,如果数据库调用出来就改这里
$info = $doc->createelement("info"); //创建节点对象实体
$info = $root->appendchild($info); //把节点添加到root节点的子节点
$namevalue = $doc->createattribute("value"); //创建节点属性对象实体
$namevalue = $info->appendchild($namevalue); //把属性添加到节点info中
$name = $doc->createelement("name"); //创建节点对象实体
$name = $info->appendchild($name);
$sex = $doc->createelement("sex");
$sex = $info->appendchild($sex);
$name->appendchild($doc->createtextnode("adevy001")); //createtextnode创建内容的子节点,然后把内容添加到节点中来
$namevalue->appendchild($doc->createtextnode("adevy"));
$sex->appendchild($doc->createtextnode(iconv("gb2312", "utf-8", "男"))); //注意要转码对于中文,因为xml默认为utf-8格式
// }
$doc->save("info.xml"); //保存路径eg d:/www.phprm.com
echo "生成成功。。。。";
//code by coder_apex 2007-6-15
//自动生成一个如下的xml文件
//
//       <?xml version="1.0" encoding="gb2312" ? >
//         - <root>
//             - <info value="www.phprm.com">
//                <name>adevy001</name>
//                <sex>男</sex>
//               </info>
//           </root>
?>


永久链接:

转载随意!带上文章地址吧。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools