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

php生成xml实例方法

May 25, 2016 pm 04:41 PM
php generate 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>
?>
Copy after login


永久链接:

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

Statement of this Website
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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)