标题: 介绍 Pear::XML_Serializer
cid73
版主
Rank: 7Rank: 7Rank: 7



UID 40
精华 2
积分 25
帖子 32
翻译 0
原创 0
阅读权限 100
注册 2007-5-23
状态 离线
发表于 2007-5-24 03:04  资料  短消息  加为好友 
介绍 Pear::XML_Serializer

在 AJAX 应用中我们经常需要向客户端传送 XML, 现在介绍一个工具 Pear::XML_Serializer

安装方法:

#pear install --force XML_Serializer

因为 XML_Serializer 没有发布 stable 版本, 我们要加上 --force 参数

假设我们通过数据库获得如下数组:

$employees = array(
  array(
    'name' => 'Jimi',
    'department' => 'Sales',
    'email' => 'jimi@company.com'
  ),
  array(
    'name' => 'Cindy',
    'department' => 'Financial',
    'email' => 'cindy@company.com'
  ),
  array(
    'name' => 'Ruby',
    'department' => 'Marketing',
    'email' => 'ruby@company.com'
  )
);

我们可以通过 XML_Serializer 转换出一个 XML 文档:

require_once 'XML/Serializer.php';
// 初始化
$serializer = new XML_Serializer(array(
  "indent"          => "\t",        // 缩进, 也可为空格
  "linebreak"       => "\n",        // 换行符
  "typeHints"       => false,       // 附带每个节点的 PHP 类型
  "addDecl"         => true,        // 加入 XML 文档头
  "encoding"        => "UTF-8",     // 文档编码
  "rootName"        => "employees", // 根节点名称
  "defaultTagName"  => "employee"   // 默认节点名称
));

$serializer->serialize($employees);

header('Content-Type: application/xml');

echo $serializer->getSerializedData();

结果如下:

<?xml version="1.0" encoding="UTF-8"?>
<employees>
        <employee>
                <name>Jimi</name>
                <department>Sales</department>
                <email>jimi@company.con</email>
        </employee>
        <employee>
                <name>Cindy</name>
                <department>Financial</department>
                <email>cindy@company.con</email>
        </employee>
        <employee>
                <name>Ruby</name>
                <department>Marketing</department>
                <email>ruby@company.con</email>
        </employee>
</employees>


顶部
Haohappy
超级版主
Rank: 8Rank: 8
PHPEye站长


UID 2
精华 11
积分 110
帖子 288
翻译 6
原创 1
阅读权限 150
注册 2007-5-2
状态 离线
发表于 2007-5-24 08:42  资料  短消息  加为好友  添加 haohappy 为MSN好友 通过MSN和 haohappy 交谈
Good,确实非常实用,不然手写XML太累了。但不知道效率如何。

顶部
smallcat
新手上路
Rank: 1



UID 65
精华 2
积分 20
帖子 10
翻译 0
原创 0
阅读权限 10
注册 2007-6-9
状态 离线
发表于 2007-6-11 20:16  资料  短消息  加为好友  添加 smallcat 为MSN好友 通过MSN和 smallcat 交谈
不错。可以试用一下。谢谢楼主

顶部
hambu363
新手上路
Rank: 1



UID 21374
精华 0
积分 0
帖子 3
翻译 0
原创 0
阅读权限 10
注册 2011-1-4
状态 离线
发表于 2011-1-6 12:12  资料  短消息  加为好友 
Chanel Sunglasses--新建文章15



Chanel Sunglasses Pblueprintctive Ski Goggles Cc Logo On Temples T4330
These brands have yieldn affliction to accommodate people with the advantages of altered ranges of sunglasses. The best allotment of these sunglasses is the appearances and admeasurements that they appear in. Keeping the superior of the lens, in the best class,Chanel Sunglasses, if the appearance is adaptd,www.chanel-sunglasses-online.com, again the acceptedity 502a732b93accomplishment6ac97b1071e1eed6bds. This is what the brands of articulatelasses have done.
The
Serengeti Sunglasses have appropriateized photo bright lens. This provides the abrasioner a ascendancy over the cast of the blush of the lenses. This acquiesces the concealment or abating of the color accordanceing to the sunray acuteness. But the atonelete range of artefacts of the Bolle sunglasses has fabricated a bazaar for themselves. And all this is becould cause they have been able to analyze the style agencys of the customers.
Tactuality are sunbottlees for anybody, alphaing from developeds to the kids. The advanced ambit of articles allures the chumps to the brands as they are abiding to acquisition something which apparel their aftertaste. Moreover,Gucci Sunglasses GG, these gdamseles accept approved to absorb anytimey new addition into their archetypals. So humans have got the taste of anti-blaze glasses from these brands. Aladmitting cher, these casts have captivated to the acceptance atom by giving the best in the business to tbeneficiary barter.

顶部
 


PHPEye开源社区


当前时区 GMT+8, 现在时间是 2012-2-10 10:16

    Powered by Discuz! 5.5.0  © 2001-2007 Comsenz Inc.
Processed in 0.030726 second(s), 8 queries , Gzip enabled

清除 Cookies - 联系我们 - PHPEye开源社区 - Archiver