Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default Delete and edit data in xml file using 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

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    So Do you know how delete and edit data from an XML file by just Java class?
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,438
    Rep Power
    18

    Default

    Yes.
    You have to get away from the idea that you should be doing this stuff in a JSP page.

    Write a Java class that does this.
    Call it from a Servlet.
    Use JSPs for displaying results...nothing more.

  4. #4
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    I dont know how to delete data but to delete the element or child node,but this program deletes single child node from start of XML document,what if i need to delete child node having given some id,for eg <tasks> is root node in xml and <task> child node having <taskid> as its further child node and i want to delete child node having taskid=1,then how it can be done(same xml file shown below program)?Please tell me how to do in JSP
    For deleting an element : RemoveElement.java
    Java Code:
    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);
        }
      }
    }
    Sample XML file : tasks.XML
    Java Code:
    <?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>
    Last edited by gurpreet.singh; 03-09-2011 at 09:05 AM.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,438
    Rep Power
    18

    Default

    You want to learn about XPath.
    The w3c dom package has an evaluator I think.

  6. #6
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    @tolls :
    thank you sir
    Please you modify the program and provide the result,ill be thankful to you
    Gurpreet Singh

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,438
    Rep Power
    18

    Default

    That's not how it works.
    As I said, you want to learn about XPath.
    So go and learn about it.

    This isn't a code factory.

Similar Threads

  1. Add , edit ,delete code in struts
    By sandy1000 in forum Web Frameworks
    Replies: 0
    Last Post: 12-28-2010, 06:25 AM
  2. I want to add, search, edit, delete record of employee
    By jacob.pham in forum New To Java
    Replies: 2
    Last Post: 07-16-2010, 06:22 AM
  3. Replies: 1
    Last Post: 11-10-2008, 09:49 AM
  4. How to Modify,Delete data in File Txt???
    By hungleon88 in forum Advanced Java
    Replies: 9
    Last Post: 09-24-2008, 03:19 AM
  5. Doubt in edit and delete methods
    By ai_2007 in forum Advanced Java
    Replies: 3
    Last Post: 07-01-2007, 11:40 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •