很多站长朋友们都不太清楚phpxml源码,今天小编就来给大家整理phpxml源码,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 求几个xml+php的源码,越简单越好 2、 php生成xml代码 3、 php生成xml代码快说把 求几个xml+php的源码,越简单越好两个,第二个简单一点
1、xml+php动态载入与分页
//$this->iGlobal($vars,$cookie);
$this->outPut($vars,$cookie);
}
//function _shoplist_res(){
// $this->db->Close();
//}
function outPut($vars,$cookie){
/*$strQuery = "select count(*) as rc from ".$this->db_c->Db_Pre."pshop order by id desc";
$this->db->Query($strQuery);
$RecordCount = $this->db->Assoc();
$RecordCount = $RecordCount['rc'];
@$this->db->freeResult();*/
$RecordCount = 25;
if (empty($vars['pageNo'])) $vars['pageNo']= 1;
if (empty($vars['pageSize'])) $vars['pageSize']= 10;
$PageCount = ceil($RecordCount/$vars['pageSize']);
if ($vars['pageSize']>$RecordCount) $vars['pageSize']=$RecordCount;
if ($vars['pageNo']>$PageCount) $vars['pageNo']=$PageCount;
$xmlStr.= "<?xml version=\"1.0\" encoding=\"GBK\" ?>"."\n";
$xmlStr.= "<商铺列表>"."\n";
$xmlStr.= "\t<翻页工具 pageNo=\"".$vars['pageNo']."\" pageSize=\"".$vars['pageSize']."\" total=\"".$RecordCount."\"></翻页工具>"."\n";
$xmlStr.= "\t<页号码>".$vars['pageNo']."</页号码>"."\n";
$xmlStr.= "\t<页大小>".$vars['pageSize']."</页大小>"."\n";
$xmlStr.= "\t<数量>".$RecordCount."</数量>"."\n";
$xmlStr.= "\t<页数量>".$PageCount."</页数量>"."\n";
//$strQuery = "select t1.*,t2.* from ".$this->db_c->Db_Pre."pshop as t1,".$this->db_c->Db_Pre."shoptype as t2 where t1.typenum=t2.typenum order by id desc "
// ."limit ".($vars['pageNo']-1)*$vars['pageSize'].",".$vars['pageSize'];
//die($strQuery);
//$this->db->Query($strQuery);
/*while($result = $this->db->Assoc()){
$xmlStr.= "\t"."\n";
$xmlStr.= "\t<商铺>"."\n";
$xmlStr.= "\t\t<名称><![CDATA[".$result['shopname']."]]></名称>"."\n";
$xmlStr.= "\t\t<经营人><![CDATA[".$result['shopmaster']."]]></经营人>"."\n";
$xmlStr.= "\t\t<级别><![CDATA[".$result['shoptype']."]]></级别>"."\n";
$xmlStr.= "\t\t<简介><![CDATA[".$result['shopintro']."]]></简介>"."\n";
$xmlStr.= "\t\t<连接地址><![CDATA[personal_shop.php?id=".$result['id']."]]></连接地址>"."\n";
$xmlStr.= "\t</商铺>"."\n";
}*/
$sc= $vars['pageNo']*$vars['pageSize'];
if ($sc>$RecordCount) $sc = $RecordCount;
for($i=($vars['pageNo']-1)*$vars['pageSize'];$i<$sc;$i++){
$xmlStr.= "\t"."\n";
$xmlStr.= "\t<商铺>"."\n";
$xmlStr.= "\t\t<名称><![CDATA[这是名称:".$i."]]></名称>"."\n";
$xmlStr.= "\t\t<经营人><![CDATA[这是经营人:".$i."]]></经营人>"."\n";
$xmlStr.= "\t\t<级别><![CDATA[这是级别:".$i."]]></级别>"."\n";
$xmlStr.= "\t\t<简介><![CDATA[这是简介:".$i."]]></简介>"."\n";
$xmlStr.= "\t\t<连接地址><![CDATA[personal_shop.php?id=".$i."]]></连接地址>"."\n";
$xmlStr.= "\t</商铺>"."\n";
}
$xmlStr.= "</商铺列表>"."\n";
header("Content-type: text/xml\n\n");
echo $xmlStr;
}
}
$shoplist = new shoplist_res($vars,$HTTP_COOKIE_VARS);
//$shoplist->_shoplist_res();
unset($sthoplist);
?>
2、用php读取xml数据
<?php
$parser = xml_parser_create(); //创建一个parser编辑器
xml_set_element_handler($parser, "startElement", "endElement");//设立标签触发时的相应函数 这里分别为startElement和endElenment
xml_set_character_data_handler($parser, "characterData");//设立数据读取时的相应函数
$xml_file="1.xml";//指定所要读取的xml文件,可以是url
$filehandler = fopen($xml_file, "r");//打开文件
while ($data = fread($filehandler, 4096))
{
xml_parse($parser, $data, feof($filehandler));
}//每次取出4096个字节进行处理
fclose($filehandler);
xml_parser_free($parser);//关闭和释放parser解析器
$name=false;
$position=false;
function startElement($parser_instance, $element_name, $attrs) //起始标签事件的函数
{
global $name,$position;
if($element_name=="NAME")
{
$name=true;
$position=false;
echo "名字:";
}
if($element_name=="POSITION")
{$name=false;
$position=true;
echo "职位:";
}
}
function characterData($parser_instance, $xml_data) //读取数据时的函数
{
global $name,$position;
if($position)
echo $xml_data."<br>";
if($name)
echo $xml_data."<br>";
}
function endElement($parser_instance, $element_name) //结束标签事件的函数
{
global $name,$position;
$name=false;
$position=false;
}
?>
xml文件代码如下:
<?xml version="1.0"?>
<employees>
<employee>
<name>张三</name>
<position age="45">经理</position>
</employee>
<employees>
<employee>
<name>李四</name>
<position age="45">助理</position>
</employee>
</employees>
php生成xml代码$docu=new DOMDocument('1.0','utf-8');//声明DOMDocument对象
$docu->formatOutput=true;//使用xml标准化格式输出
$request=$docu->createElement('request');//使用createElement创建一个request标签
$action=$docu->createElement('action');//在request标签下创建action标签
$value=$docu->createTextNode('regAndenter');//在action里插入字符串
$action->appendChild($value);//
$request->appendChild($action);//将创建的action标签添加到xml文件里
$xmlid=$docu->createElement('id');
$value=$docu->createTextNode($dataid);
$xmlid->appendChild($value);
$request->appendChild($xmlid);
$phonenumber =$docu->createElement('phonenumber');
$value=$docu->createTextNode($uname);
$phonenumber->appendChild($value);
$request->appendChild($phonenumber);
$password =$docu->createElement('password');
$value=$docu->createTextNode($pass);
$password->appendChild($value);
$request->appendChild($password);
$email=$docu->createElement('email');
$value=$docu->createTextNode($emai);
$email->appendChild($value);
$request->appendChild($email);
$realname =$docu->createElement('realname');
$value=$docu->createTextNode($name1);
$realname->appendChild($value);
$request->appendChild($realname);
$document_id =$docu->createElement('document_id');
$value=$docu->createTextNode($idc);
$document_id->appendChild($value);
$request->appendChild($document_id);
$provincecode=$docu->createElement('provincecode');
$value=$docu->createTextNode($privince);
$provincecode->appendChild($value);
$request->appendChild($provincecode);
$citycode =$docu->createElement('citycode');
$value=$docu->createTextNode($city);
$citycode->appendChild($value);
$request->appendChild($citycode);
$grouptype =$docu->createElement('grouptype');
$value=$docu->createTextNode($type);
$grouptype->appendChild($value);
$request->appendChild($grouptype);
$college =$docu->createElement('college');
$value=$docu->createTextNode($colle);
$college->appendChild($value);
$request->appendChild($college);
$docu->appendChild($request);//将创建的request标签添加到xml文件里
//$docu->save('wang.xml');//生成xml文件
照着我这个改,希望你能看得懂
php生成xml代码快说把使用PHP DOMDocument创建动态XML文件
当处理基于XML应用程序时,开发者经常需要建立XML编码数据结构。例如,Web中基于用户输入的XML状态模板,服务器请求XML语句,以及基于运行时间参数的客户响应。
尽管XML数据结构的构建比较费时,但如果使用成熟的PHP DOM应用程序接口,一切都会变得简单明了。本文将向你介绍PHP DOM应用程序接口的主要功能,演示如何生成一个正确的XML完整文件并将其保存到磁盘中。
创建文档类型声明
一般而言,XML声明放在文档顶部。在PHP中声明十分简单:只需实例化一个DOM文档类的对象并赋予它一个版本号。查看程序清单A:
程序清单 A
复制代码 代码如下:
<?php
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// save and display tree
echo $dom->saveXML();
?>
请注意DOM文档对象的saveXML()方法。稍后我再详细介绍这一方法,现在你只需要简单认识到它用于输出XML文档的当前快照到一个文件或浏览器。在本例,为增强可读性,我已经将ASCII码文本直接输出至浏览器。在实际应用中,可将以text/XML头文件发送到浏览器。
如在浏览器中查看输出,你可看到如下代码:
<?xml version="1.0"?>
添加元素和文本节点
XML真正强大的功能是来自其元素与封装的内容。幸运的是,一旦你初始化DOM文档,很多操作变得很简单。此过程包含如下两步骤:
对想添加的每一元素或文本节点,通过元素名或文本内容调用DOM文档对象的createElement()或createTextNode()方法。这将创建对应于元素或文本节点的新对象。
通过调用节点的appendChild()方法,并把其传递给上一步中创建的对象,并在XML文档树中将元素或文本节点添加到父节点。
以下范例将清楚地演示这2步骤,请查看程序清单B。
程序清单 B
复制代码 代码如下:
<?php
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// save and display tree
echo $dom->saveXML();
?>
这 里,我首先创建一个名字为<toppings>的根元素,并使它归于XML头文件中。然后,我建立名为<item>的元素并使它 归于根元素。最后,我又创建一个值为“pepperoni”的文本节点并使它归于<item>元素。最终结果如下:
复制代码 代码如下:
<?xml version="1.0"?>
<toppings>
<item>pepperoni</item>
</toppings>
如果你想添加另外一个topping,只需创建另外一个<item>并添加不同的内容,如程序清单C所示。
程序清单C
复制代码 代码如下:
<?php
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create another text node
$text = $dom->createTextNode("tomato");
$item->appendChild($text);
// save and display tree
echo $dom->saveXML();
?>
以下是执行程序清单C后的输出:
复制代码 代码如下:
<?xml version="1.0"?>
<toppings>
<item>pepperoni</item>
<item>tomato</item>
</toppings>
添加属性
通过使用属性,你也可以添加适合的信息到元素。对于PHP DOM API,添加属性需要两步:首先用DOM文档对象的createAttribute()方法创建拥有此属性名字的节点,然后将文档节点添加到拥有属性值的属性节点。详见程序清单D。
程序清单 D
复制代码 代码如下:
<?php
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// create attribute node
$price = $dom->createAttribute("price");
$item->appendChild($price);
// create attribute value node
$priceValue = $dom->createTextNode("4");
$price->appendChild($priceValue);
// save and display tree
echo $dom->saveXML();
?>
输出如下所示:
复制代码 代码如下:
<?xml version="1.0"?>
<toppings>
<item price="4">pepperoni</item>
</toppings>
添加CDATA模块和过程向导
虽然不经常使用CDATA模块和过程向导,但是通过调用DOM文档对象的createCDATASection()和createProcessingInstruction()方法, PHP API 也能很好地支持CDATA和过程向导,请见程序清单E。
程序清单 E
复制代码 代码如下:
<?php
// create doctype
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// create attribute node
$price = $dom->createAttribute("price");
$item->appendChild($price);
// create attribute value node
$priceValue = $dom->createTextNode("4");
$price->appendChild($priceValue);
// create CDATA section
$cdata = $dom->createCDATASection(" Customer requests that pizza be sliced into 16 square pieces ");
$root->appendChild($cdata);
// create PI
$pi = $dom->createProcessingInstruction("pizza", "bake()");
$root->appendChild($pi);
// save and display tree
echo $dom->saveXML();
?>
输出如下所示:
复制代码 代码如下:
<?xml version="1.0"?>
<toppings>
<item price="4">pepperoni</item>
<![CDATA[
Customer requests that pizza be sliced into 16 square pieces
]]>
<?pizza bake()?>
</toppings>
保存结果
一旦已经实现你的目标,就可以将结果保存在一个文件或存储于PHP的变量。通过调用带有文件名的save()方法可以将结果保存在文件中,而通过调用saveXML()方法可存储于PHP的变量。请参考以下实例(程序清单F):
程序清单 F
复制代码 代码如下:
<?php
// create doctype
$dom = new DOMDocument("1.0");
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
$dom->formatOutput=true;
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// create attribute node
$price = $dom->createAttribute("price");
$item->appendChild($price);
// create attribute value node
$priceValue = $dom->createTextNode("4");
$price->appendChild($priceValue);
// create CDATA section
$cdata = $dom->createCDATASection(" Customer requests that pizza be
sliced into 16 square pieces ");
$root->appendChild($cdata);
// create PI
$pi = $dom->createProcessingInstruction("pizza", "bake()");
$root->appendChild($pi);
// save tree to file
$dom->save("order.xml");
// save tree to string
$order = $dom->save("order.xml");
?>
下面是实际的例子,大家可以测试下。
xml.php(生成xml)
复制代码 代码如下:
<?
$conn = mysql_connect('localhost', 'root', '123456') or die('Could not connect: ' . mysql_error());
mysql_select_db('vdigital', $conn) or die ('Can\'t use database : ' . mysql_error());
$str = "SELECT id,username FROM `admin` GROUP BY `id` ORDER BY `id` ASC";
$result = mysql_query($str) or die("Invalid query: " . mysql_error());
if($result)
{
$xmlDoc = new DOMDocument();
if(!file_exists("01.xml")){
$xmlstr = "<?xml version='1.0' encoding='utf-8' ?><message></message>";
$xmlDoc->loadXML($xmlstr);
$xmlDoc->save("01.xml");
}
else { $xmlDoc->load("01.xml");}
$Root = $xmlDoc->documentElement;
while ($arr = mysql_fetch_array($result)){
$node1 = $xmlDoc->createElement("id");
$text = $xmlDoc->createTextNode(iconv("GB2312","UTF-8",$arr["id"]));
$node1->appendChild($text);
$node2 = $xmlDoc->createElement("name");
$text2 = $xmlDoc->createTextNode(iconv("GB2312","UTF-8",$arr["username"]));
$node2->appendChild($text2);
$Root->appendChild($node1);
$Root->appendChild($node2);
$xmlDoc->save("01.xml");
}
}
mysql_close($conn);
?>
test.php(应用测试)
复制代码 代码如下:
<?
$xmlDoc = new DOMDocument();
$xmlDoc->load("");
$x=$xmlDoc->getElementsByTagName('name');
for ($i=0; $i<=$x->length-1; $i++)
{
if(strpos($x->item($i)->nodeValue,"fang")!==false)
{
echo $x->item($i)->parentNode->childNodes->item(1)->nodeValue;
}
}
?>
关于phpxml源码的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于phpxml源码 php源码怎么使用的详细内容...