
I want to know how to delete and edit data from an XML file by use of JSP. I have XML file having tasks(root node),task id,project,date(as child nodes). I have many tasks stored in the xml file,I want to delete and edit some tasks using task id then how can i do it in JSP.Please help with example if any.Thank you XML file : tasks.XML
<?xml version = '1.0' encoding = 'UTF-8'?>
<Tasks>
<task>
<Taskid>1</Taskid>
<Taskname>Coding</Taskname>
<Project>CeMIC</Project>
<Date>21 February</Date>
</task>
<task>
<Taskid>2</Taskid>
<Taskname>Testing</Taskname>
<Project>Blackberry</Project>
<Date>2 march</Date>
</task>
<task>
<Taskid>3</Taskid>
<Taskname>Integration</Taskname>
<Project>Assinment JSP</Project>
<Date>23 march</Date>
</task>
<task>
<Taskid>4</Taskid>
<Taskname>Implementation</Taskname>
<Project>CMIC</Project>
<Date>7 april</Date>
</task>
<task>
<Taskid>5</Taskid>
<Taskname>Maintainance</Taskname>
<Project>CMIC</Project>
<Date>7 april</Date>
</task>
</Tasks>
I want JSP code so that i can delete data. For exmple JSP pae will have text box where ill enter TASK ID(Enter taskid to delete) and it will delete that id and relatd other nodes to that task id from that xml file. Same way If i can edit the nodes.Please ell JSP code for that,.Ill be very thankful to you

Java code to implement this but how to make it work in JSP? RemoveElement.java :
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
public class RemoveElement {
static public void main(String[] arg) {
try{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a XML file name: ");
String xmlFile = bf.readLine();
File file = new File(xmlFile);
System.out.print("Enter an element which have to delete: ");
String remElement = bf.readLine();
if (file.exists()){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(xmlFile);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer tFormer = tFactory.newTransformer();
Element element = (Element)doc.getElementsByTagName(remElement).item(0);
// Remove the node
element.getParentNode().removeChild(element);
// Normalize the DOM tree to combine all adjacent nodes
doc.normalize();
Source source = new DOMSource(doc);
Result dest = new StreamResult(System.out);
tFormer.transform(source, dest);
System.out.println();
}
else{
System.out.println("File not found!");
}
}
catch (Exception e){
System.err.println(e);
System.exit(0);
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.