Android上解析.plist配置文件的xml解析器
来源:互联网 发布:win7联想网络驱动下载 编辑:程序博客网 时间:2024/05/22 21:42
plist是苹果机制的xml文件
格式如下:
<plistversion="1.0">
<array>
<dict>
<key>countryDomain</key>
<string>HK</string>
<key>code</key>
<string>852</string>
<key>country</key>
<string>Hong Kong</string>
</dict>
<dict>
<key>countryDomain</key>
<string>CN</string>
<key>code</key>
<string>86</string>
<key>country</key>
<string>China</string>
</dict>
</array>
</plist>
我相信很多人都是在做iphone的软件转移到android平台去,要怎么解析呢?我找到了一个牛人写的辅助android下解析这个苹果机制的xml的工具类
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
/**
*解析苹果机制下的plist
* @author chen_weihua
*/
public class PlistHandler extends DefaultHandler {
private LinkedList<Object> list = new LinkedList<Object>();
//是否为根标签
private boolean isRootElement = false;
//标签开始
private boolean keyElementBegin = false;
//键
private String key;
//值开始
private boolean valueElementBegin = false;
//根对象
private Object root;
@SuppressWarnings("unchecked")
public Map getMapResult() {
return (Map)root;
}
@SuppressWarnings("unchecked")
public List getArrayResult() {
return (List)root;
}
@SuppressWarnings("unchecked")
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes)throws SAXException {
if("plist".equals(localName)) {
isRootElement =true;
}
if("dict".equals(localName)) {
if(isRootElement) {
list.addFirst(new HashMap());
isRootElement = !isRootElement;
}else{
ArrayList parent = (ArrayList)list.get(0);
list.addFirst(new HashMap());
parent.add(list.get(0));
}
}
if("key".equals(localName)) {
keyElementBegin =true;
}
if("true".equals(localName)) {
HashMap parent = (HashMap)list.get(0);
parent.put(key,true);
}
if("false".equals(localName)) {
HashMap parent = (HashMap)list.get(0);
parent.put(key,false);
}
if("array".equals(localName)) {
if(isRootElement) {
ArrayList obj =new ArrayList();
list.addFirst(obj);
isRootElement = !isRootElement;
}else{
HashMap parent = (HashMap)list.get(0);
ArrayList obj =new ArrayList();
list.addFirst(obj);
parent.put(key, obj);
}
}
if("string".equals(localName)) {
valueElementBegin =true;
}
}
@SuppressWarnings("unchecked")
@Override
public void characters(char[] ch, intstart,intlength)
throws SAXException {
if(length > 0) {
if(keyElementBegin) {
key =new String(ch, start, length);
Log.d("AR native","key:"+ key);
}
if(valueElementBegin) {
if(HashMap.class.equals(list.get(0).getClass())) {
HashMap parent = (HashMap)list.get(0);
String value =new String(ch, start, length);
parent.put(key, value);
}else if(ArrayList.class.equals(list.get(0).getClass())) {
ArrayList parent = (ArrayList)list.get(0);
String value =new String(ch, start, length);
parent.add(value);
}
//Log.d("AR native", "value:" + value);
}
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if("plist".equals(localName)) {
;
}
if("key".equals(localName)) {
keyElementBegin =false;
}
if("string".equals(localName)) {
valueElementBegin =false;
}
if("array".equals(localName)) {
root = list.removeFirst();
}
if("dict".equals(localName)) {
root = list.removeFirst();
}
}
}
有没有人用过呢?一起研究吧。
原工具类发布地址:http://www.pin5i.com/showtopic-android-.plist-xml.html
- Android上解析.plist配置文件的xml解析器
- android下解析.plist配置文件的xml解析器
- iOS项目配置文件info.plist文件解析上
- iOS的Plist-XML-JSON数据解析
- android解析plist文件
- android解析plist文件
- android解析plist文件
- Android Plist文件解析
- Android 解析 plist
- android解析来自服务器上的xml
- xml解析(配置文件的pull解析)
- XML配置文件的解析 写入
- Java SAX解析器解析XML配置文件
- Android配置文件Manifest.xml如何被加载、解析的
- android的xml解析
- android XML的解析
- android xml 的解析.
- Android的XML解析
- 程序员的十大谎言
- iOS开发时,出现“xcode cannot run using the selected device“错误
- SQL Server 2012中基于列的存储方式
- Apache2.4配置mod_wsgi,部署python应用
- Quartz.net 在应用程序中统一调度各任务,Config文件配置所有的任务项
- Android上解析.plist配置文件的xml解析器
- MATLAB鼠标响应函数1
- PackageManager相关文章
- iOS调试技巧
- 逆波兰表达式
- [Win32]Win32网络小结
- httphandler vs httpmodule
- Permutation Sequence
- err:module:import_dll Library MSVCP90.dll