可以,例如,使自己的Item
類,有title
,event_date
,event_start_time
和event_location
作爲變量。
做項目的數組:
ArrayList <Item> arrayItems = new ArrayList <Item>();
然後,讀取XML:
File fXmlFile = new File("/yourFile.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("item");
for (int i = 0; i < nList.getLength(); i++) {
Node nNode = nList.item(i);
Element eElement = (Element) nNode;
title= eElement.getAttribute("title"));
//event_date, event_start_time, ...
arrayItems.add(i, new Item(title, event_date,event_start_time,event_location));
}
}
}
看一看JAXP(DOM),DOM4J或JDOM解析API。 –
使用JAXB;那麼你將不必編寫任何繁瑣的解析代碼。 – rob