PHP解析XML格式的Plist文件

 

在苹果IOS开发过程中,经常会用到plist格式的文件,它是一种XML格式文件,本文主要基于xml原理来解析plist文件。

Plist 文件格式

本文解析的plist文件格式如下:

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE plist PUBLIC “-//Apple Computer//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=”1.0″>
<dict>
<dict>
<key>KEY1</key>
<string>KEY_VALUE</string>
</dict>
<key>KEY2</key>
<array>
<dict>
<key>KEY2_key1</key>
<string>KEY2_key1_value1</string>
</dict>
<dict>
<key>KEY2_key2</key>
<string>KEY2_key2_value1</string>
</dict>
</array>
</dict>
</plist>

从上面的格式我们可以通过定义xml解析函数来获取数据。

使用PHP XML 解析器函数

//会用到的全部变量
global $xml_parser,$songs,$number_dicts,$current_key,$current_element,$current_data,$end_of_songs;

//文件路径
$target = ‘FILE_Path’;
//文件名称
$filename = ‘FILE_Name’;

//建立一个 XML 解析器
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);
xml_set_element_handler($xml_parser, “start_element”, “end_element”);
xml_set_character_data_handler($xml_parser, “character_data”);

if (!($fp = @fopen($target, “r”))) {
return false;
}

while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf(“XML error: %s at line %d in file %s\n”,
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser),
$filename));
}
}

//释放指定的 XML 解析器
xml_parser_free($xml_parser);

文件主要部分负责建立XML解析器和定义元素处理器和字符数据处理器。

 

然后在建立处理器函数,如下:

function start_element($parser, $name, $attribs) {
global $current_element, $number_dicts;
if($name==”DICT”){
$number_dicts++;
}
if ($number_dicts>1){
$current_element=$name;
}
}

function end_element($parser, $name) {
global $songs, $current_element, $current_data, $number_dicts, $array_key, $end_of_songs;
if($end_of_songs){
return;
}
if(!empty($current_element)) {
if($current_element==”KEY”){
$array_key=$current_data;
}else{
$songs[$number_dicts][$array_key] .= $current_data;
}
}
}

function character_data($parser, $data) {
global $number_dicts, $current_data, $end_of_songs, $prev_data;
if($data==”Playlists”) {
$end_of_songs=TRUE;
}
$data = str_replace(array(“\r\n”), ”, $data);
$current_data=trim($data);
if(strpos($current_data, “’”) > -1 || strpos($current_data, “’”) > -1 || strpos($current_data, “,”) > -1) {
$current_data = $prev_data.$current_data;
}
else {
$prev_data = $current_data;
}
}

然后再分别定义建立起始和终止元素处理器和建立字符数据处理器。

最后这个完整的PHP文件

<?php
//全局变量
$xml_parser=””;
$songs=array();
$number_dicts=0;
$current_key=””;
$current_element=””;
$current_data=””;
$end_of_songs=FALSE;

//文件路径
$target = ‘./test.plist’;
//文件名称
$filename = ‘test.plist’;

//建立一个 XML 解析器
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);
xml_set_element_handler($xml_parser, “start_element”, “end_element”);
xml_set_character_data_handler($xml_parser, “character_data”);

if (!($fp = @fopen($target, “r”))) {
return false;
}

while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf(“XML error: %s at line %d in file %s\n”,
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser),
$filename));
}
}

//释放指定的 XML 解析器
xml_parser_free($xml_parser);

function start_element($parser, $name, $attribs) {
global $current_element, $number_dicts;
if($name==”DICT”){
$number_dicts++;
}
if ($number_dicts>1){
$current_element=$name;
}
}

function end_element($parser, $name) {
global $songs, $current_element, $current_data, $number_dicts, $array_key, $end_of_songs;
if($end_of_songs){
return;
}
if(!empty($current_element)) {
if($current_element==”KEY”){
$array_key=$current_data;
}else{
$songs[$number_dicts][$array_key] .= $current_data;
}
}
}

function character_data($parser, $data) {
global $number_dicts, $current_data, $end_of_songs, $prev_data;
if($data==”Playlists”) {
$end_of_songs=TRUE;
}
$data = str_replace(array(“\r\n”), ”, $data);
$current_data=trim($data);
if(strpos($current_data, “’”) > -1 || strpos($current_data, “’”) > -1 || strpos($current_data, “,”) > -1) {
$current_data = $prev_data.$current_data;
}
else {
$prev_data = $current_data;
}
}

//输出结果
echo ‘<pre>’;
print_r($songs);
echo ‘</pre>’;

请将其保存为php_parse_plist.php文件后,放到服务器上运行。

源码下载:php_parse_plist.php下载

 

 

标签:
,
分类:SERVER | 发布:inzaghi | 评论:0 条 | 发表时间:2014-09-24 13:25
引用:点击这里获取该日志的TrackBack引用地址
上一篇:
下一篇:

发表评论

你必须 登录后 才能对文章进行评论!

Design By Inzaghi | 京ICP备16047555号-1