Modify and delete certain xml entries
So i create an xml file wich is formated like this;
Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Events>
<Event>
<eventid>1</eventid>
<year>2010</year>
<month>09</month>
<day>19</day>
<calendarid>2</calendarid>
<eventname>birthday me</eventname>
<starthour>21</starthour>
<startminutes>00</startminutes>
<stophour>03</stophour>
<stopminutes>00</stopminutes>
<description>birthdayparty</description>
</Event>
<Event>
<eventid>2</eventid>
<year>2010</year>
<month>11</month>
<day>23</day>
<calendarid>2</calendarid>
<eventname>birthday mom</eventname>
<starthour>21</starthour>
<startminutes>00</startminutes>
<stophour>03</stophour>
<stopminutes>00</stopminutes>
<description>birthdayparty</description>
</Event>
</Events>
now what i need to do is look for the correct event by looking at it's eventid (wich is unique), and then look for a node (like starthour, description or eventname) and modify this. but i've been at this for 3 days now and i can't seem to get it fixed.
Code:
public void saveEvents(File file ,Document moDoc, String filename) throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
src = new DOMSource(moDoc);
String temp = filename + ".xml";
res = new StreamResult(new File(temp));
transformer.transform(src, res);
}
public void modEvents2(String filename, String mod, String newVal, String evId) throws Exception{
String filepath = filename + ".xml";
modDoc = getBuilder.parse(filepath);
//Element root = modDoc.getDocumentElement();
NodeList childNodes = modDoc.getElementsByTagName("Event");
for (int i = 0; i < childNodes.getLength(); i++) {
NodeList subChildNodes = childNodes.item(i).getChildNodes();
for (int j = 0; j < subChildNodes.getLength(); j++) {
Node child = (Node) subChildNodes.item(j);
if (child.getNodeName().equalsIgnoreCase("eventid") && child.getTextContent().equalsIgnoreCase(evId)) {
if (subChildNodes.item(j).getTextContent().equals(mod)) {
subChildNodes.item(j).setTextContent(newVal);
}
}
}
}
saveEvents(XmlFile, modDoc, filename);
}
any help would be greatly appreciated!