Revision: 58589
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at July 23, 2012 15:04 by l01241
Initial Code
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.gtomato.disneyhalloween2012.pojo.MapPin;
public class PlistParser {
// parse Plist and fill in arraylist
public ArrayList<MapPin> parsePlist(String xml) {
final ArrayList<MapPin> dataModels = new ArrayList<MapPin>();
// Get the xml string from assets
final Document doc = XMLfromString(xml);
final NodeList nodes_array = doc.getElementsByTagName("dict");
// Fill in the list items from the XML document
for (int index = 0; index < nodes_array.getLength(); index++) {
final Node node = nodes_array.item(index);
if (node.getNodeType() == Node.ELEMENT_NODE) {
final Element e = (Element) nodes_array.item(index);
final NodeList nodeKey = e.getElementsByTagName("key");
final NodeList nodeValue = e.getElementsByTagName("string");
// MapPin model = new MapPin();
for (int i = 0; i < nodeValue.getLength(); i++) {
final Element eleKey = (Element) nodeKey.item(i);
final Element eleString = (Element) nodeValue.item(i);
if (eleString != null) {
String strValue = getValue(eleString, "string");
if (getValue(eleKey, "key").equals("gameid")) {
//model = new MapPin();
//model.setGameId(Integer.parseInt(strValue));
} else if (getValue(eleKey, "key").equals("drawid")) {
//model.setDrawId(Integer.parseInt(strValue));
} else if (getValue(eleKey, "key").equals("dname")) {
//model.setDrawName(strValue);
} else if (getValue(eleKey, "key").equals("date")) {
//model.setDate(strValue);
} else if (getValue(eleKey, "key").equals("prize")) {
//model.setPrize(strValue);
} else if (getValue(eleKey, "key").equals("sels")) {
if (strValue == null) {
strValue = "";
}
//model.setSels(strValue);
//dataModels.add(model);
}
}
}
}
}
return dataModels;
}
// Create xml document object from XML String
private Document XMLfromString(String xml) {
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
System.out.println("XML parse error: " + e.getMessage());
return null;
} catch (SAXException e) {
System.out.println("Wrong XML file structure: " + e.getMessage());
return null;
} catch (IOException e) {
System.out.println("I/O exeption: " + e.getMessage());
return null;
}
return doc;
}
// fetch value from Text Node only
private String getElementValue(Node elem) {
Node kid;
if (elem != null) {
if (elem.hasChildNodes()) {
for (kid = elem.getFirstChild(); kid != null; kid = kid
.getNextSibling()) {
if (kid.getNodeType() == Node.TEXT_NODE) {
return kid.getNodeValue();
}
}
}
}
return "";
}
// / Fetch value from XML Node
private String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return getElementValue(n.item(0));
}
}
Initial URL
Initial Description
Initial Title
android plist parser
Initial Tags
android
Initial Language
Other