473,480 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

10 New Member
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 4207
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
May i know why you want to do it in JSP?

Regards
Dheeraj Joshi
Mar 9 '11 #2
Gurpreet Singhh
10 New Member
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
Arunkrishna
1 New Member
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
Gurpreet Singhh
10 New Member
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
5130
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
2010
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
24651
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
3331
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
2406
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
2384
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
2021
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
1210
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
14585
by: Coffee Pot | last post by:
Thanks for any advice. ~ CP
1
1259
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
6915
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6750
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5353
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4493
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2993
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
567
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.