使用具有相似值的 NodeList
usin NodeList with similar values
我有以下 xml 文件来存储我的事件:
<event
day="23"
month="4"
year="2015"
mod="mod1" >
event 1
</event>
<event
day="23"
month="4"
year="2015"
mod="mod1" >
event 2
</event>
<event
day="24"
month="6"
year="2015"
mod="mod2" >
event 3
</event>
<event
day="24"
month="7"
year="2015"
mod="mod3" >
event 4
</event>
并使用此方法加载事件:
private List<Event> events;
public void loadEvents(InputStream xmlStream) {
events = new ArrayList<Event>();
DocumentBuilder builder;
try {
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = builder.parse(xmlStream);
NodeList eventsNodes = document.getElementsByTagName("event");
for (Node node : new IterableNodeList(eventsNodes)) {
NamedNodeMap attrs = node.getAttributes();
int year = Integer.parseInt(attrs.getNamedItem("year")
.getNodeValue());
int month = Integer.parseInt(attrs.getNamedItem("month")
.getNodeValue());
int day = Integer.parseInt(attrs.getNamedItem("day")
.getNodeValue());
String mod = attrs.getNamedItem("mod").getNodeValue();
String eventTitle = node.getFirstChild().getNodeValue();
if(mod.equals("mod1")){
events.add(new Event(new CivilDate(year, month, day),
eventTitle + "mod1"));
}
if(mod.equals("mod2")){
events.add(new Event(new CivilDate(year, month, day),
eventTitle + "mod2"));
}
if(mod.equals("mod3")){
events.add(new Event(new CivilDate(year, month, day),
eventTitle + "mod3"));
}
}
} catch (ParserConfigurationException e) {
Log.e("ERR", e.getMessage());
} catch (SAXException e) {
Log.e("ERR", e.getMessage());
} catch (IOException e) {
Log.e("ERR", e.getMessage());
}
}
这是获取事件的方法:
public String getEventTitle(CivilDate date) {
for (Event event : events) {
if (event.getDate().equals(date)) {
// trim XML whitespaces and newlines
return event.getTitle().replaceAll("\n", "").trim();
}
}
return null;
}
但我有问题...在我的 xml 文件中我有 2 个相似的日期和不同的事件如果我尝试在同一日期获取事件它的 returns 只有 "event 1"。
如果日期匹配,你做一个return,所以只有第一个是return。
public ArrayList<String> getEventTitle(CivilDate date) {
ArrayList<String> events = new ArrayList<String>();
for (Event event : events) {
if (event.getDate().equals(date)) {
// trim XML whitespaces and newlines
events.add(event.getTitle().replaceAll("\n", "").trim());
}
}
return events ;
}
我有以下 xml 文件来存储我的事件:
<event
day="23"
month="4"
year="2015"
mod="mod1" >
event 1
</event>
<event
day="23"
month="4"
year="2015"
mod="mod1" >
event 2
</event>
<event
day="24"
month="6"
year="2015"
mod="mod2" >
event 3
</event>
<event
day="24"
month="7"
year="2015"
mod="mod3" >
event 4
</event>
并使用此方法加载事件:
private List<Event> events;
public void loadEvents(InputStream xmlStream) {
events = new ArrayList<Event>();
DocumentBuilder builder;
try {
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = builder.parse(xmlStream);
NodeList eventsNodes = document.getElementsByTagName("event");
for (Node node : new IterableNodeList(eventsNodes)) {
NamedNodeMap attrs = node.getAttributes();
int year = Integer.parseInt(attrs.getNamedItem("year")
.getNodeValue());
int month = Integer.parseInt(attrs.getNamedItem("month")
.getNodeValue());
int day = Integer.parseInt(attrs.getNamedItem("day")
.getNodeValue());
String mod = attrs.getNamedItem("mod").getNodeValue();
String eventTitle = node.getFirstChild().getNodeValue();
if(mod.equals("mod1")){
events.add(new Event(new CivilDate(year, month, day),
eventTitle + "mod1"));
}
if(mod.equals("mod2")){
events.add(new Event(new CivilDate(year, month, day),
eventTitle + "mod2"));
}
if(mod.equals("mod3")){
events.add(new Event(new CivilDate(year, month, day),
eventTitle + "mod3"));
}
}
} catch (ParserConfigurationException e) {
Log.e("ERR", e.getMessage());
} catch (SAXException e) {
Log.e("ERR", e.getMessage());
} catch (IOException e) {
Log.e("ERR", e.getMessage());
}
}
这是获取事件的方法:
public String getEventTitle(CivilDate date) {
for (Event event : events) {
if (event.getDate().equals(date)) {
// trim XML whitespaces and newlines
return event.getTitle().replaceAll("\n", "").trim();
}
}
return null;
}
但我有问题...在我的 xml 文件中我有 2 个相似的日期和不同的事件如果我尝试在同一日期获取事件它的 returns 只有 "event 1"。
如果日期匹配,你做一个return,所以只有第一个是return。
public ArrayList<String> getEventTitle(CivilDate date) {
ArrayList<String> events = new ArrayList<String>();
for (Event event : events) {
if (event.getDate().equals(date)) {
// trim XML whitespaces and newlines
events.add(event.getTitle().replaceAll("\n", "").trim());
}
}
return events ;
}