Last active
August 29, 2015 14:03
-
-
Save likebamboo/ea6946bc60403446f693 to your computer and use it in GitHub Desktop.
简单的plist文件解析
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.liwt.parser; | |
import org.xml.sax.Attributes; | |
import org.xml.sax.SAXException; | |
import org.xml.sax.helpers.DefaultHandler; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Stack; | |
import javax.xml.parsers.ParserConfigurationException; | |
import javax.xml.parsers.SAXParser; | |
import javax.xml.parsers.SAXParserFactory; | |
/** | |
* .plist配置文件的解析器 | |
* 支持array | |
* <plist version="1.0"> | |
* <array> | |
* <dict> ... </dict> | |
* ... | |
* </array> | |
* </plist> | |
* 支持Map | |
* <plist version="1.0"> | |
* <dict> | |
* <id>key</id> | |
* <array> | |
* <dict> ... </dict> | |
* ... | |
* </array> | |
* ... | |
* </dict> | |
* </plist> | |
* | |
* @author likebamboo | |
*/ | |
public class PlistParser extends DefaultHandler { | |
private boolean isRootElement = false; | |
private boolean keyElementBegin = false; | |
private String key; | |
Stack<Object> stack = new Stack<Object>(); | |
private boolean valueElementBegin = false; | |
private Object root; | |
@SuppressWarnings("unchecked") | |
public HashMap<String, Object> getMapResult() { | |
return (HashMap<String, Object>)root; | |
} | |
@SuppressWarnings("unchecked") | |
public List<Object> getArrayResult() { | |
return (List<Object>)root; | |
} | |
@Override | |
public void startDocument() throws SAXException { | |
System.out.println("开始解析"); | |
} | |
@Override | |
public void endDocument() throws SAXException { | |
System.out.println("结束解析"); | |
} | |
@SuppressWarnings("unchecked") | |
@Override | |
public void startElement(String uri, String localName, String qName, Attributes attributes) | |
throws SAXException { | |
// System.out.println(uri+"startElement:"+qName); | |
if ("plist".equals(qName)) { | |
isRootElement = true; | |
} | |
if ("dict".equals(qName)) { | |
if (isRootElement) { | |
stack.push(new HashMap<String, Object>());// 压栈 | |
isRootElement = !isRootElement; | |
} else { | |
Object object = stack.peek(); | |
HashMap<String, Object> dict = new HashMap<String, Object>(); | |
if (object instanceof ArrayList) | |
((ArrayList<Object>)object).add(dict); | |
else if (object instanceof HashMap) | |
((HashMap<String, Object>)object).put(key, dict); | |
stack.push(dict); | |
} | |
} | |
if ("key".equals(qName)) { | |
keyElementBegin = true; | |
} | |
if ("true".equals(qName)) { | |
HashMap<String, Object> parent = (HashMap<String, Object>)stack.peek(); | |
parent.put(key, true); | |
} | |
if ("false".equals(qName)) { | |
HashMap<String, Object> parent = (HashMap<String, Object>)stack.peek(); | |
parent.put(key, false); | |
} | |
if ("array".equals(qName)) { | |
if (isRootElement) { | |
ArrayList<Object> obj = new ArrayList<Object>(); | |
stack.push(obj); | |
isRootElement = !isRootElement; | |
} else { | |
HashMap<String, Object> parent = (HashMap<String, Object>)stack.peek(); | |
ArrayList<Object> obj = new ArrayList<Object>(); | |
stack.push(obj); | |
parent.put(key, obj); | |
} | |
} | |
if ("string".equals(qName)) { | |
valueElementBegin = true; | |
} | |
} | |
/* | |
* 字符串解析(non-Javadoc) | |
* @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int) | |
*/ | |
@SuppressWarnings("unchecked") | |
@Override | |
public void characters(char[] ch, int start, int length) throws SAXException { | |
if (length > 0) { | |
if (keyElementBegin) { | |
key = new String(ch, start, length); | |
System.out.println("key:" + key); | |
} | |
if (valueElementBegin) { | |
if (HashMap.class.equals(stack.peek().getClass())) { | |
HashMap<String, Object> parent = (HashMap<String, Object>)stack.peek(); | |
String value = new String(ch, start, length); | |
parent.put(key, value); | |
} else if (ArrayList.class.equals(stack.peek().getClass())) { | |
ArrayList<Object> parent = (ArrayList<Object>)stack.peek(); | |
String value = new String(ch, start, length); | |
parent.add(value); | |
} | |
System.out.println("value:" + new String(ch, start, length)); | |
} | |
} | |
} | |
@Override | |
public void endElement(String uri, String localName, String qName) throws SAXException { | |
if ("plist".equals(qName)) { | |
; | |
} | |
if ("key".equals(qName)) { | |
keyElementBegin = false; | |
} | |
if ("string".equals(qName)) { | |
valueElementBegin = false; | |
} | |
if ("array".equals(qName)) { | |
root = stack.pop(); | |
} | |
if ("dict".equals(qName)) { | |
root = stack.pop(); | |
} | |
} | |
public static void main(String[] args) { | |
SAXParserFactory factorys = SAXParserFactory.newInstance(); | |
SAXParser saxparser = null; | |
try { | |
saxparser = factorys.newSAXParser(); | |
} catch (ParserConfigurationException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (SAXException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
PlistParser plistHandler = new PlistParser(); | |
try { | |
saxparser.parse("citydict.plist", plistHandler); | |
} catch (SAXException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
HashMap<String, Object> hash = (HashMap<String, Object>)plistHandler.getMapResult(); | |
// ArrayList<Object> array = | |
// (ArrayList<Object>)plistHandler.getArrayResult(); | |
System.out.println(hash); | |
System.out.println(plistHandler.toInsertSql(hash, "city")); | |
} | |
private String toInsertSql(HashMap<String, Object> datas, String tableName) { | |
if (datas == null || datas.size() == 0) { | |
return ""; | |
} | |
StringBuilder sb = new StringBuilder(); | |
for (Iterator it = datas.keySet().iterator(); it.hasNext();) { | |
String key = (String)it.next(); | |
ArrayList<String> citys = (ArrayList<String>)datas.get(key); | |
for (int i = 0; i < citys.size(); i++) { | |
sb.append("insert into " + tableName + " values( null, '" + citys.get(i) + "','" | |
+ key + "');\n"); | |
} | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment