2
所以我試圖做的是循環通過我下面顯示的XML文件並將內容保存到每個「屏幕」的對象。通過循環遍歷XML構建一個對象
我的XML如下:
<?xml version="1.0" encoding="utf-8"?>
<screens>
<screen id="house" backdropimage = "R.drawable.scene">
<region id="one" rleft = "200" rright = "200" rtop = "10" rbottom = "10"/>
<region id="two" rleft = "220" rright = "220" rtop = "12" rbottom = "12"/>
<paths left = "park" right = "x" top = "x" bottom = "x"/>
</screen>
<screen id="park" backdropimage = "R.drawable.park">
<region id="1" rleft = "500" rright = "200" rtop = "10" rbottom = "10"/>
<paths right = "house" left = "x" top = "x" bottom = "x"/>
</screen>
</screens>
這樣的想法是,我在陣列屏幕的在每個屏幕塊的信息創建一個對象。身份證和背景都很好,但即使在「區域」部分迭代也存在問題,因爲在不同部分會有不同數量的區域條目。這是即時通訊使用的代碼,由於某種原因,它通過「一」「兩」和「1」,我希望它停在/屏幕上。
depth = parser.getDepth();
while(parser.getDepth() == depth) {
if(event==XmlPullParser.START_TAG && parser.getName().contentEquals("region")) {
Region reg = new Region(parser.getAttributeIntValue(null, "rleft", 0),
parser.getAttributeIntValue(null, "rtop", 0),
parser.getAttributeIntValue(null, "rright", 0),
parser.getAttributeIntValue(null, "rbottom", 0));
//String str = parser.getIdAttribute();
newScreen.area = (reg);
//Rect temp = reg.getBounds();
newScreen.areaArray.add(reg);
parser.next();
}
這是我第一次使用的Java/Android的XML文件的嘗試,我還是蠻新到Java總體上,所以我敢肯定,我沒有使用最好的方法在這裏和將不勝感激任何幫助。謝謝
非常欣賞這種信息,原來我的代碼是工作我如何預期它我只是讀輸出不正確。 – Be77amy
@tdtje:優秀的答案btw。 –
@tdtje我一直在android中解析xml,很高興我偶然發現了這篇文章。這個庫似乎比使用pull解析器好得多。除非解析速度太慢,否則我會切換到這種方法。謝謝。 –