Java Parse plist file

Source: Internet
Author: User
Reproduced from: http://archive.cnblogs.com/a/2391527/

To easily import the Spritesheet diagram into my own animation editor.
I did the plist file parsing
Dom parsing is tricky because element getchildnodes gets the text object. And this object may be a blank character parsing up an exceptionally troublesome.

DOM uses a tree-structured approach to accessing XML documents, while SAX uses the event model.


The DOM parser converts an XML document into a tree containing its contents, and can traverse the tree. The advantage of using the DOM parsing model is that it is easy to program, the developer only needs to invoke the build instructions, and then use the navigation APIs to access the required tree nodes to complete the task. You can easily add and modify elements in the tree. However, because of the need to process the entire XML document with the DOM parser, the performance and memory requirements are high, especially when you encounter a large XML file. Because of its traversal capabilities, DOM parsers are often used in services where XML documents require frequent changes.


The SAX parser employs an event-based model that triggers a series of events when parsing an XML document, and when a given tag is found, it can activate a callback method that tells the method that the label has been found. SAX typically requires less memory because it lets developers decide which tag they want to handle. Especially when developers only need to work with some of the data contained in the document, SAX has a better ability to expand. But coding is difficult when using a SAX parser, and it is difficult to access multiple different data in the same document at the same time.

DOM [tree-structured] SAX [event-driven]
Then you use Sax parsing. Someone has written on the internet, but there are some problems, so they have done a revision test. The release code is as follows:

Package Logame.core;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.LinkedList;
Import java.util.List;
Import Java.util.Map;

Import Java.util.Stack;
Import org.xml.sax.Attributes;
Import org.xml.sax.SAXException;

Import Org.xml.sax.helpers.DefaultHandler; /** *. plist configuration File Parser * * @author afly */public class Plisthandler extends DefaultHandler {Private Boolean is

	RootElement = 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 ("start parsing"); } @Override public void ENDDOCUment () throws saxexception {System.out.println ("End Resolution"); @SuppressWarnings ("unchecked") @Override public void startelement (string uri, String localname, String qName, Attrib
		Utes 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> ());//Press Stack IsRo
			Otelement =!isrootelement;
				else {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; }/* * string parsing (non-javadoc) * * @see org.xml.sax.helpers.defaulthandler#characters (char[], int, int) * * * @Supp Resswarnings ("unchecked") @Override public void characters (char[] ch, int start, int length) throws Saxexception {Sys
		Tem.out.println ("characters:"); 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&LT;OBJECT&G
					t;) 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 ("Plis
		T ". 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 (); }
	}
} 

The call is also relatively simple:

	SAXParserFactory Factorys = Saxparserfactory.newinstance ();
	SAXParser saxparser = Factorys.newsaxparser ();
	Plisthandler Plisthandler = new Plisthandler ();
	Saxparser.parse (URI, Plisthandler);

	Hashmap<string, object> hash = Plisthandler.getmapresult ();
	arraylist<object> array =  (arraylist<object>) Plisthandler.getarrayresult ();
Return can be a dict or an array can be obtained according to its own format.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.