[英]How can a put a one child element value into an ArrayList in Android?
我對標記所有元素的 XmlPullParser 事件有困難,但是如何將一個子元素存儲到 ArrayList 中?
這是我的 xml 文件:
<site>
<name>Brake and Break</name>
<link>https://sampleactivity.000webhostapp.com/lesson_1.mp3</link>
<set>When teaching my daughter how to drive, I told her if she didn't hit the brake in time she would break the car's side mirror.</set>
<set>You shouldn’t brake as often as you do. You are going to break your car’s brakes.</set>
<set>I think this relationship is moving too fast for me. Maybe it's best to just hit the brake than to completely break up.</set>
<image>https://sampleactivity.000webhostapp.com/stckOvflw.png</image>
</site>
在我的 xml 文件中,我有元素名稱SET那么如何將所有設置的元素和屬性存儲到數組列表中?
這是我的 xmlpullparser.java 代碼:
switch (eventType) {
case XmlPullParser.START_TAG:
if (tagname.equalsIgnoreCase(KEY_SITE)) {
curStackSite = new StackSite();
}
break;
case XmlPullParser.TEXT:
curText = xpp.getText();
break;
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase(KEY_SITE)) {
stackSites.add(curStackSite);
} else if (tagname.equalsIgnoreCase(KEY_NAME)) {
curStackSite.setName(curText);
} else if (tagname.equalsIgnoreCase(KEY_LINK)) {
curStackSite.setLink(curText);
} else if (tagname.equalsIgnoreCase(KEY_SET)) {
ArrayList<String> wew = new ArrayList<String>();
wew.add(curText);
curStackSite.setAlAbout(wew); <--- How can I store all element to arraylist here?
curStackSite.setSet(curText);
} else if (tagname.equalsIgnoreCase(KEY_IMAGE_URL)) {
curStackSite.setImgUrl(curText);
}
break;
default:
break;
}
我已經嘗試過對我們進行 dom 解析,但在我的代碼中仍然沒有太大的作用。
更新:
這是我的堆棧站點值:
public class StackSite {
private String name;
private String link;
private String about;
private List<String> AlAbout;
private String id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getAbout() {
return about;
}
public void setAbout(String about) {this.about = about;}
public List<String> getAlAbout() {
return AlAbout;
}
public void setAlAbout(List<String> AlAbout) {this.AlAbout = AlAbout;}
@Override
public String toString() {
return "StackSite [name=" + name + ", link=" + link + ", about="
+ about + ", Array sets=" + AlAbout + "]";
}
}
如果StackSite
class 有這樣的成員:
List<String> allSets;
allSets
的構造函數將StackSite
初始化為一個空列表:
allSets = new ArrayList<>();
然后在END_TAG
情況下tagName.equalsIgnoreCase(KEY_SET)
您只需添加設置文本:
curStackSite.allSets.add(curText);
將為遇到的每個<set>…</set>
在allSets
列表中添加一個單獨的條目。
在解析StackSite
的allSets
列表成員結束時,包含 0 個或多個集合內容條目。
至於屬性,您可能希望為包含String
集文本和屬性字段的StackSiteSet
創建一個新的 class,而不是將集合視為字符串。
因此,您對</set>
的END_TAG
處理應如下所示:
} else if (tagname.equalsIgnoreCase(KEY_SET)) {
// Get the list and add another element to it.
curStackSite.getAlAbout().add(curText);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.