1
嗨,我想我已經使用KPBirds的plist解析器的修改版本在這裏找到http://kpbird.blogspot.co.uk/2011/08/android-plist-parser.html的Android解析的plist字符串和整數
問題是我擁有的一切設置,沒有錯誤解析在Android上的plist但是當我運行它試圖通過整數解析字符串。例如:名稱被設置爲一個字符串,並通過說,名稱不能被解析爲一個整數。
有什麼想法爲什麼?
繼承人我的代碼到目前爲止它的工作方式是在我的活動中,我運行一個函數,獲取讀取plist然後應用數據模型,然後應該將其登錄到日誌貓。
繼承人的錯誤
09-02 10:49:44.717: E/AndroidRuntime(1736): Caused by: java.lang.NumberFormatException: unable to parse 'image1' as integer
09-02 10:49:44.717: E/AndroidRuntime(1736): at java.lang.Integer.parse(Integer.java:383)
09-02 10:49:44.717: E/AndroidRuntime(1736): at java.lang.Integer.parseInt(Integer.java:372)
09-02 10:49:44.717: E/AndroidRuntime(1736): at java.lang.Integer.parseInt(Integer.java:332)
09-02 10:49:44.717: E/AndroidRuntime(1736): at co.uk.luke.ParsePlist.parsePlist(ParsePlist.java:68)
09-02 10:49:44.717: E/AndroidRuntime(1736): at co.uk.luke.LevelSelect.readPlistContents(LevelSelect.java:63)
09-02 10:49:44.717: E/AndroidRuntime(1736): at co.uk.luke.LevelSelect.onCreate(LevelSelect.java:91)
09-02 10:49:44.717: E/AndroidRuntime(1736): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
09-02 10:49:44.717: E/AndroidRuntime(1736): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)
繼承人我的活動在哪裏調用函數
/// Read plist from Assets
private String readPlistFromAssets() {
StringBuffer sb = new StringBuffer();
BufferedReader br=null;
try {
br = new BufferedReader(new InputStreamReader(getAssets().open("Levels.plist")));
String temp;
while ((temp = br.readLine()) != null)
sb.append(temp);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close(); // stop reading
} catch (IOException ex) {
ex.printStackTrace();
}
}
return sb.toString();
}
public void readPlistContents(){
// Read Plist content
String xml = readPlistFromAssets();
// create object of Parser Class
ParsePlist pp = new ParsePlist();
// parse Plist and fill in plist array
ArrayList<DataModel> plist = pp.parsePlist(xml);
// Print ArrayList
Iterator<DataModel> i = plist.iterator();
while(i.hasNext()){
DataModel d = i.next();
Log.i("Plist","=========================");
Log.i("Plist","Levelid = " + d.getLevelid());
Log.i("Plist","title = " + d.getTitle());
Log.i("Plist","name = " + d.getName());
Log.i("Plist","lockStatus = " + d.getLockStatus());
Log.i("Plist","total = " + d.getTotal());
Log.i("Plist","totalCompleted = " + d.getTotalCompleted());
Log.i("Plist","Points = " + d.getPoints());
Log.i("Plist","Image = " + d.getImage());
}
Toast.makeText(this, "Parsing completed please check logcat...", Toast.LENGTH_LONG).show();
}
繼承人我的數據模型
public class DataModel {
private String title;
private String name;
private String levelid;
private String lockStatus;
private int total;
private int totalCompleted;
private int Points;
private String Image;
public int getTotaCompleted() {
return totalCompleted;
}
public void setTotalCompleted(int totalCompleted) {
this.totalCompleted = totalCompleted;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getPoints() {
return Points;
}
public void setPoints(int Points) {
this.Points = Points;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLevelid() {
return levelid;
}
public void setLevelid(String levelid) {
this.levelid = levelid;
}
public String getLockStatus() {
return lockStatus;
}
public void setLockStatus(String lockStatus) {
this.lockStatus = lockStatus;
}
public String getImage() {
return Image;
}
public void setImage(String Image) {
this.Image = Image;
}
}
繼承人在那裏我解析的plist代碼
public class ParsePlist {
// parse Plist and fill in arraylist
public ArrayList<DataModel> parsePlist(String xml) {
final ArrayList<DataModel> dataModels = new ArrayList<DataModel>();
//Get the xml string from assets
final Document doc = XMLfromString(xml);
final NodeList nodes_array = doc.getElementsByTagName("array");
//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 nodeString = e.getElementsByTagName("string");
DataModel model = new DataModel();
for (int i=0; i<nodeString.getLength(); i++) {
final Element eleKey = (Element)nodeKey.item(i);
final Element eleString = (Element)nodeString.item(i);
if (eleString != null) {
String strValue = getValue(eleString, "string");
if(getValue(eleKey, "key").equals("title")) {
model = new DataModel();
model.setTitle(strValue);
} else if(getValue(eleKey, "key").equals("name")) {
model.setName(strValue);
} else if(getValue(eleKey, "key").equals("levelid")) {
model.setLevelid(strValue);
} else if(getValue(eleKey, "key").equals("lockStatus")) {
model.setLockStatus(strValue);
} else if(getValue(eleKey, "key").equals("total")) {
model.setTotal(Integer.parseInt(strValue));
} else if(getValue(eleKey, "key").equals("totalCompleted")) {
model.setTotalCompleted(Integer.parseInt(strValue));
} else if(getValue(eleKey, "key").equals("Points")) {
model.setPoints(Integer.parseInt(strValue));
} else if(getValue(eleKey, "key").equals("Image")) {
model.setImage(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));
}
}
最後繼承人我的plist
<key>title</key>
<string>ZONE1</string>
<key>name</key>
<string>LEVEL 1</string>
<key>levelid</key>
<string>1</string>
<key>lockStatus</key>
<string>UNLOCKED</string>
<key>total</key>
<integer>20</integer>
<key>totalCompleted</key>
<integer>0</integer>
<key>Points</key>
<integer>0</integer>
<key>Image</key>
<string>image1</string>
添加我這是它目前表示,任何noteInteger未使用 –
我又不能隨便補充說,因爲它沒有被用到任何地方我需要在某處調用eleinteger –
將它稱爲第二個for循環 –