Home > php教程 > php手册 > PHP解析XML格式的Plist文件

PHP解析XML格式的Plist文件

WBOY
Release: 2016-06-06 20:08:40
Original
3501 people have browsed it

在苹果IOS开发过程中,经常会用到plist格式的文件,它是一种XML格式文件,本文主要基于xml原理来解析plist文件。 Plist 文件格式 本文解析的plist文件格式如下: ?xml version="1.0" encoding="UTF-8"? !DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST

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

Plist 文件格式

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

<?xml version="1.0" encoding="UTF-8"?>  <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>
Copy after login

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

PHP xml parse plist

使用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);
Copy after login

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

(...)
Read the rest of PHP解析XML格式的Plist文件 (340 words)


© lixiphp for LixiPHP - 致力于PHP高级编程!, 2013. | Permalink | No comment | Add to del.icio.us
Post tags: PHP, plist, XML

Feed enhanced by Better Feed from Ozh

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template