473,386 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to delete and edit data from an XML file by use of JSP?

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::
Expand|Select|Wrap|Line Numbers
  1. <?xml version = '1.0' encoding = 'UTF-8'?>
  2. <Tasks>
  3.    <task>
  4.       <Taskid>1</Taskid>
  5.       <Taskname>Coding</Taskname>
  6.       <Project>CeMIC</Project>
  7.       <Date>21 February</Date>
  8.    </task>
  9.    <task>
  10.       <Taskid>2</Taskid>
  11.       <Taskname>Testing</Taskname>
  12.       <Project>Blackberry</Project>
  13.       <Date>2 march</Date>
  14.    </task>
  15.    <task>   
  16.       <Taskid>3</Taskid>
  17.       <Taskname>Integration</Taskname>
  18.       <Project>Assinment JSP</Project>
  19.       <Date>23 march</Date>
  20.    </task>
  21.    <task>
  22.       <Taskid>4</Taskid>
  23.       <Taskname>Implementation</Taskname>
  24.       <Project>CMIC</Project>
  25.       <Date>7 april</Date>
  26.    </task>
  27.    <task>
  28.       <Taskid>5</Taskid>
  29.       <Taskname>Maintainance</Taskname>
  30.       <Project>CMIC</Project>
  31.       <Date>7 april</Date>
  32.    </task>
  33. </Tasks>
  34.  
I have a java program to remove element but i want to know how to remove by providing element attribute (like task id) and deleting all data IN JSP?
RemoveElement.java: I want it in jsp

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import org.w3c.dom.*;
  3. import org.xml.sax.*;
  4. import javax.xml.parsers.*;
  5. import javax.xml.transform.*; 
  6. import javax.xml.transform.dom.DOMSource; 
  7. import javax.xml.transform.stream.StreamResult;
  8.  
  9. public class RemoveElement {
  10.   static public void main(String[] arg) {
  11.     try{
  12.       BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  13.       System.out.print("Enter a XML file name: ");
  14.       String xmlFile = bf.readLine();
  15.       File file = new File(xmlFile);
  16.       System.out.print("Enter an element which have to delete: ");
  17.       String remElement = bf.readLine();
  18.       if (file.exists()){
  19.         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  20.         DocumentBuilder builder = factory.newDocumentBuilder();
  21.         Document doc = builder.parse(xmlFile);
  22.         TransformerFactory tFactory = TransformerFactory.newInstance();
  23.         Transformer tFormer = tFactory.newTransformer();
  24.         Element element = (Element)doc.getElementsByTagName(remElement).item(0);
  25. //        Remove the node
  26.         element.getParentNode().removeChild(element);
  27. //              Normalize the DOM tree to combine all adjacent nodes
  28.         doc.normalize();
  29.         Source source = new DOMSource(doc);
  30.         Result dest = new StreamResult(System.out);
  31.         tFormer.transform(source, dest);
  32.         System.out.println();
  33.       }
  34.       else{
  35.         System.out.println("File not found!");
  36.       }
  37.     }
  38.     catch (Exception e){
  39.       System.err.println(e);
  40.       System.exit(0);
  41.     }
  42.   }
  43.  
Mar 9 '11 #1
4 4197
Dheeraj Joshi
1,123 Expert 1GB
May i know why you want to do it in JSP?

Regards
Dheeraj Joshi
Mar 9 '11 #2
because I want to implement it on web,I am learning JSP as web technologies and I have an assignment to complete in which i need to delete and edit XML data using JSP
Mar 9 '11 #3
i think this will help u
Expand|Select|Wrap|Line Numbers
  1. import java.io.File;
  2. import javax.xml.parsers.DocumentBuilder;
  3. import javax.xml.parsers.DocumentBuilderFactory;
  4. import org.w3c.dom.*;
  5.  
  6. public class AccessingXmlFile {
  7.  
  8.  public static void main(String argv[]) {
  9.  
  10.   try {
  11.   File file = new File("C:\\MyFile.xml");
  12.   DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  13.   DocumentBuilder db = dbf.newDocumentBuilder();
  14.   Document document = db.parse(file);
  15.   document.getDocumentElement().normalize();
  16.   System.out.println("Root element "+document.getDocumentElement().getNodeName());
  17.   NodeList node = document.getElementsByTagName("student");
  18.   System.out.println("Information of the students");
  19.  
  20.   for (int i = 0; i < node.getLength(); i++) {
  21.   Node firstNode = node.item(i);
  22.  
  23.   if (firstNode.getNodeType() == Node.ELEMENT_NODE) {
  24.  
  25.   Element element = (Element) firstNode;
  26.   NodeList firstNameElemntList = element.getElementsByTagName("firstname");
  27.   Element firstNameElement = (Element) firstNameElemntList.item(0);
  28.   NodeList firstName = firstNameElement.getChildNodes();
  29.   System.out.println("First Name:"+ ((Node)firstName.item(0).getNodeValue());
  30.  
  31.   NodeList lastNameElementList = element.getElementsByTagName("lastname");
  32.   Element lastNameElement = (Element) lastNameElementList.item(0);
  33.   NodeList lastName = lastNameElement.getChildNodes();
  34.   System.out.println("Last Name :"+((Node)lastName.item(0).getNodeValue());
  35.  
  36.   NodeList addressList = element.getElementsByTagName("address");
  37.   Element addressElement = (Element) addressList.item(0);
  38.   NodeList address = addressElement.getChildNodes();
  39.   System.out.println("Address : "  + ((Node) address.item(0)).getNodeValue());
  40.  
  41.   NodeList cityList = element.getElementsByTagName("city");
  42.   Element cityElement = (Element) cityList.item(0);
  43.   NodeList city = cityElement.getChildNodes();
  44.   System.out.println("City : "  + ((Node) city.item(0)).getNodeValue());
  45.  }
  46. }
  47.   } catch (Exception e) {
  48.     e.printStackTrace();
  49.   }
  50.  }
  51. }
Mar 9 '11 #4
Thanks for the reply
Sir your program is giving the errors
")" in line 29
after i removed that error its giving another errornamed as : line 31 & 36 : inconvertible types
Please check it again and remove errors and reply back
Thank you
Mar 10 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Trevor Davies | last post by:
I have a database which I have loaded onto our as/400 v4r5. All the tables and data have loaded successfully. I am using DB2 connect to access. I now want to delete the data in a table which is...
4
by: T.Jackson | last post by:
Hi guys, I want to display the data of a table in a datagrid in one form, & enable the user to edit the details of a particular record in another form. I want the following features, 1....
1
by: ortega.rad | last post by:
I have a form which allows you to select a record. That record has other records asscociated with it via a table. The asscociated records of the record selected on the main form are shown in a...
2
by: bro | last post by:
hi everybody, i currently develop an app under c#. i want to play the last x seconds of a previously recorded and saved wav-file. there is a simple way to achieve this? If not, is there any...
1
by: BiT | last post by:
Hello I Know how to create and add elements to xml file with XmlTextWriter, here is code for example: Dim textWriter As XmlTextWriter = New XmlTextWriter("table.xml", Nothing) ' Opens the...
4
by: Sanchit | last post by:
I want to know thta how can i edit a file in C++ For Example my file is Mr XyZ FFFFFF 65 And now i want go change this number 65 to 87.... how can i Do this..... I...
1
by: fishjelly | last post by:
How to edit and delete the data stored in xml file using C# and visual studio 2005 through user input? For example: for this xml document... <MenuRoot> <Books> <book> <title> ABC </title>...
0
by: Andy B | last post by:
I have a GridView in asp.net. I want to set its datasource to some xml content. At the same time, I need to know how to insert/delete/edit content in the GridView rows and then repopulate the xml...
18
by: Coffee Pot | last post by:
Thanks for any advice. ~ CP
1
by: inciguleray | last post by:
I want to knw that how to edit a file? how to delete a whole file? how to add some text at a specified location in a file ? how to delete something from a specified location in a file?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.