本文章提供二种生成xml的方法,第一种是直接查询数据库,查询在php页面输出xml格式的数据,第二种方法是利用了php DOMDocument组件生成xml实例原理有一点不同.
php生成xml实例方法代码如下:
<?php $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' />"; } //利用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> ?>
永久链接:
转载随意!带上文章地址吧。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download
The most popular open source editor
