将数组转化为XML数据
/* Author: 杨宇 yangyu@sina.cn */
/*
用法示例:
$cls_xml = new cls_xml();
if ($array){
$cls_xml->array2xml($array);
echo $cls_xml->getXml();
}else{
echo '';
}
*/
class cls_xml{
var $xml;
public function array2xml($array,$encoding='gb2312'){
$this->xml = '<?xml version="1.0" encoding="'.$encoding.'"?><list>';
$this->xml.= $this->_array2xml($array).'</list>';
}
public function getXml(){
return $this->xml;
}
public function _array2xml($array){
$xml = '';
foreach($array as $key => $val){
is_numeric($key) && $key = "item id=\"$key\"";
$xml. = "<$key>";
$xml. = is_array($val) ? $this->_array2xml($val) : $this->_cdata($val);
list($key,) = explode(' ',$key);
相关文档:
XML Schema annotation 元素
定义和用法
annotation 元素是一个顶层元素,规定 schema 的注释。
注释:可以包含 appinfo 元素(由应用程序使用的信息)和 documentation 元素(由用户读取或使用的注释或文本)。
元素信息
项目
说明
出现次数
在父元素中一次。
......
XML解析器的作用:为应用程序从XML文件中解析出所需要的数据。
下面通过一个例子,来了解,如何用XML解析器,来解析一个XML文件中的数据。
1、Types.xml(显示吉他的类别)
<?xml version="1.0" encoding="UTF-8"?>
<types>
<name>电吉他
<music>玩摇滚</music>
</name>
......
范例如下:
var xml:XML=
<body>
text1
<bar>barText1</bar>
& ......
2)如何创建一个xml文档
由于xml实质也只是一个文本文件,所以最简单你可以直接使用System.IO下的类生成一个文件,并存储成xml文件,当然,你需要手动保证该文件形式良好,比如必须有根元素、必须有关闭标签、必须正确嵌套等等。
如果你懒得自己去想文件的形式,可以使用System.Xml下的类。
Code
Xml ......
public class yzzSerialize
{
private yzzSerialize()
{ }
private static yzzCache cache = new yzzCache();
public static T GetfromXml<T>(string xmlpath, T t)
{
using (FileStream fs = new FileStream(xmlpath, FileMode.Open, FileAcces ......