Rumah > php教程 > php手册 > teks badan

PHP类CFPropertyList操作Plist文件

WBOY
Lepaskan: 2016-06-06 20:09:01
asal
3173 orang telah melayarinya

本文主要讲解PHP类CFPropertyList来实现Plist文件的读、写、修改,Plist介绍和PHP Library CFPropertyList的介绍。 plist介绍 Plist的全称是Property lists,是一种用来存储串行化后的对象的文件。属性列表文件的文件扩展名为 .plist ,因此通常被称为 plist

本文主要讲解PHP类CFPropertyList来实现Plist文件的读、写、修改,Plist介绍和PHP Library CFPropertyList的介绍。

plist介绍

Plist的全称是Property lists,是一种用来存储串行化后的对象的文件。属性列表文件的文件扩展名为.plist,因此通常被称为plist文件。Plist文件通常用于储存用户设置,也可以用于存储捆绑的信息。

Plist组织数据到命名值和列表值,主要通过几个主要的Core Foundation类型:CFString, CFNumber, CFBoolean, CFDate, CFData, CFArray, 和 CFDictionary。

Plist结构和内容

Property lists从基本的Core Foundation 类型:CFString,CFNumber,CFBoolean,CFDate,CFData构造。要建立一个复杂的数据结构从这些基本类型,你得把它们放在里面CFDictionary或CFArray里面。为了简化对Property lists的编程,任何属性列表类型也可以被引用通过使用类型CFPropertyListRef。

在一个CFDictionary,数据结构是以键-值对的形式,其中每个键是一个字符串,该键的值可以是一个CFString字符串,一个CFNumber,一个CFBoolean,一个CFDate,一个CFData,一个CFArray,或其他CFDictionary。当使用CFDictionary作为属性列表时,所有的键必须是字符串。

在一个CFArray,数据结构是以一个可以通过索引访问的对象的有序集合。在属性列表中,一个CFArray可以包含任何的基本属性列表类型,以及CFDictionary和其他CFArray的对象。

PROPERTY LIST XML 标签

当属性列表将Core Foundation对象集合转换成一个XML的属性列表,使用文件类型标签来包含所有的属性列表。下表中列出Core Foundation数据类型常用的其他标记:

Core Foundation数据类型等同的XML

Core Foundation类型

XML标签

CFString

CFNumber

或者

CFBoolean

或者

CFDate

CFData

CFArray

CFDictionary

PHP类CFPropertyList

通过PHP来实现苹果Property list,PHP类CFPropertyList不仅可以处理XML PropertyLists,而且也可以处理二进制Property Lists。它提供的功能可以轻松地转换数据到PHP中,例如重新计算时间戳从Unix纪元到苹果纪元,反之亦然。它有一个重大的功能就是从一个正常的PHP数据结构自动地创建为Property List数据结构,它将帮助你输出你的数据到plist中在任何时候。

PHP Plist CFPropertyList PHP类CFPropertyList

运行环境

CFPropertyList不依赖任何“苹果专有的”组件像plutil。 CFPropertyList运行在任何具有PHP的操作系统和一些标准PHP扩展。

虽然您可能希望将数据传送到你的iPhone应用程序,你可能想运行这些服务器端的服务在标准的Linux(甚至Windows)环境作为一种服务,而不是购买昂贵的苹果服务器上。随着CFPropertyList的发展,你现在有能力提供数据从您最喜爱的操作系统。

要求和限制

  • 需要PHP5.3(为CFPropertyList2.0)
  • 需要Mbstring或Iconv PHP扩展

类库列表及功能

  • CFPropertyList.php,CFPropertyList核心处理,包括读、写、修改和保存PropertyList。
  • CFBinaryPropertyList.php,读写二进制格式的Property Lists的工具。从苹果CFBinaryPList.c移植而来。
  • CFType.php,CFPropertyList所使用的所有CFTypes的基础类。
  • CFTypeDetector.php,转换本地PHP的数据结构到CFPropertyList对象的接口。
  • IOException.php,基本输入/ 输出异常。
  • PListException.php,Plist格式的异常错误。

PHP类CFPropertyList 使用方法

从Plist文件中读取数据到PHP数组

/**
* 对于如何使用CFPropertyList例子
* 读取一个XML PropertyList
* @package plist
* @subpackage plist.examples
*/
namespace CFPropertyList;
// 调试
error_reporting( E_ALL );
ini_set( ‘display_errors’, ‘on’ );
/**
* Require CFPropertyList
*/
require_once(__DIR__.’/../classes/CFPropertyList/CFPropertyList.php’);
/*
* 创建一个新的CFPropertyList实例,它构造时装载sample.plist
* 因为我们知道这是一个XML文件,我们可以跳过格式判断
*/
$plist = new CFPropertyList( __DIR__.’/lesson002.plist’, CFPropertyList::FORMAT_XML );
/*
* 从sample.plist获取数组结构并输出
*/
print_r($plist->toArray());
echo ‘

’; <br>var_dump( $plist->toArray() ); <br>echo ‘
Salin selepas log masuk
’;
?>

通过CFPropertyList plist API将PHP数据写入到Plist文件

/**
* 对于如何使用CFPropertyList例子
* 通过使用CFPropertyList plist API创建PropertyList sample.xml.plist。
* @package plist
* @subpackage plist.examples
*/
namespace CFPropertyList;
// 调试
error_reporting( E_ALL );
ini_set( ‘display_errors’, ‘on’ );
/**
* Require CFPropertyList
*/
require_once(__DIR__.’/../classes/CFPropertyList/CFPropertyList.php’);
/*
* 创建一个新的并没有加载任何内容的CFPropertyList实例
*/
$plist = new CFPropertyList();
/*
* 手动创建sample.xml.plist
*/
// PList的根元素是一个Dictionary
$plist->add( $dict = new CFDictionary() );
// Year Of Birth1965
$dict->add( ‘Year Of Birth’, new CFNumber( 1965 ) );
// Date Of Graduation2004-06-22T19:23:43Z
$dict->add( ‘Date Of Graduation’, new CFDate( gmmktime( 19, 23, 43, 06, 22, 2004 ) ) );
// Pets Names
$dict->add( ‘Pets Names’, new CFArray() );
// PicturePEKBpYGlmYFCPA==
// 为了保持简单,我们插入一个已经base64编码的字符串
$dict->add( ‘Picture’, new CFData( ‘PEKBpYGlmYFCPA==’, true ) );
// City Of BirthSpringfield
$dict->add( ‘City Of Birth’, new CFString( ‘Springfield’ ) );
// NameJohn Doe
$dict->add( ‘Name’, new CFString( ‘John Doe’ ) );
// Kids NamesJohnKyra
$dict->add( ‘Kids Names’, $array = new CFArray() );
$array->add( new CFString( ‘John’ ) );
$array->add( new CFString( ‘Kyra’ ) );
/*
* 保存为 PList 为 XML文件
*/
$plist->saveXML( __DIR__.’/example-create-01.xml.plist’ );
/*
* 保存为 PList 为二进制文件
*/
$plist->saveBinary( __DIR__.’/example-create-01.binary.plist’ );
?>

(...)
Read the rest of PHP类CFPropertyList操作Plist文件 (210 words)


© lixiphp for LixiPHP, 2013. | Permalink | No comment | Add to del.icio.us
Post tags: CFPropertyList, library, PHP, plist, PropertyList, 数据结构

Feed enhanced by Better Feed from Ozh

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!