Results 1 to 4 of 4
  1. #1
    lokeshnaalla is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Code to delete a particular record

    Quote Originally Posted by gurpreet.singh View Post
    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>
    Hi Guru preet,

    This is lokesh kumar,

    I am also looking the for the same functionality.
    I want code to delete a particular record based on some id.
    Please give me the code if you have already implemented for your requirement.

    Thanks in advance,
    Regards,
    Lokesh

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Code to delete a particular record

    Removed from Delete and edit data in xml file using JSP

    When you have a question, start a new thread. Don't post to old dead threads and don't hijack another poster's thread.

    Also note that this is a forum, not a free code service, as already mentioned in the thread you hijacked. If you wish to learn, show us what you already know and where you're stuck and someone who is knowledgeable in that area may help you to understand how to proceed, and guide you to get rid of any errors you may encounter.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    lokeshnaalla is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Re: Code to delete a particular record

    Hi all,

    I have an xml file like this

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!-- This file contains the information about the printers -->
    <Local-Printer-Info>
    <PRINTER>
    <name>Printer1</name>
    <type>printing</type>
    <service>HP LaserJet 2300 Series PCL 6</service>
    <location>chennai</location>
    <ip-address>12.25.4.34</ip-address>
    <port>9100</port>
    </PRINTER>
    <PRINTER>
    <name>Printer2</name>
    <type>printing3</type>
    <service>HP LaserJet 2300 Series PCL 6</service>
    <location>tc3</location>
    <ip-address>12.29.197.245</ip-address>
    <port>9000</port>
    </PRINTER>
    <PRINTER>
    <name>Printer4</name>
    <type>printing</type>
    <service>HP LaserJet 2300 Series PCL 6</service>
    <location>tc3</location>
    <ip-address>17.29.197.24</ip-address>
    <port>9000</port>
    </PRINTER>
    <PRINTER>
    <name>Printer3</name>
    <type>printing</type>
    <service>HP LaserJet 2300 Series PCL 7</service>
    <location>dlf</location>
    <ip-address>17.29.197.25</ip-address>
    <port>9009</port>
    </PRINTER>
    <PRINTER>
    <name>Printer5</name>
    <type />
    <service>HP LaserJet 2300 Series PCL 7</service>
    <location />
    <ipaddress>17.29.197.22</ipaddress>
    <Port>9009</Port>
    </PRINTER>
    </Local-Printer-Info>


    My requirement is to delete a particular printer based on the printer name.
    For example if I delete Printer5, all the details about the Printer5 should be removed from the xml file.

    After deletion the xml file should be like

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!-- This file contains the information about the printers -->
    <Local-Printer-Info>
    <PRINTER>
    <name>Printer1</name>
    <type>printing</type>
    <service>HP LaserJet 2300 Series PCL 6</service>
    <location>chennai</location>
    <ip-address>12.25.4.34</ip-address>
    <port>9100</port>
    </PRINTER>
    <PRINTER>
    <name>Printer2</name>
    <type>printing3</type>
    <service>HP LaserJet 2300 Series PCL 6</service>
    <location>tc3</location>
    <ip-address>12.29.197.245</ip-address>
    <port>9000</port>
    </PRINTER>
    <PRINTER>
    <name>Printer4</name>
    <type>printing</type>
    <service>HP LaserJet 2300 Series PCL 6</service>
    <location>tc3</location>
    <ip-address>17.29.197.24</ip-address>
    <port>9000</port>
    </PRINTER>
    <PRINTER>
    <name>Printer3</name>
    <type>printing</type>
    <service>HP LaserJet 2300 Series PCL 7</service>
    <location>dlf</location>
    <ip-address>17.29.197.25</ip-address>
    <port>9009</port>
    </PRINTER>
    </Local-Printer-Info>

    I have written the below code to delete a particular printer but it is deleting only the tag information from the xml file.


    public class XMLReadWrite {
    public static void main(String[] args) throws Exception {
    // addPrinter();
    // getNodeElements();
    deleteElement();
    }
    private static void deleteElement() throws Exception{
    // TODO Auto-generated method stub
    Element root;
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
    Document doc = docBuilder.newDocument();
    File file = new File("D:/conf/printer_info.xml");
    doc = docBuilder.parse(file);
    root = doc.getDocumentElement();
    Element element = (Element)doc.getElementById("Printer5");
    element.getParentNode().removeChild(element);

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();

    transformer.setOutputProperty(OutputKeys.INDENT, "yes");


    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);
    String xmlString = sw.toString();
    FileWriter fw=new FileWriter(file,false);
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(xmlString);
    bw.flush();
    bw.close();

    }
    }

    Please help me to achieve the above scenario.

    Regards,
    Lokesh

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Code to delete a particular record

    Guide For New Members
    BB Code List - Java Programming Forum

    Couldn't you notice the code tags in the old post you quoted?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Delete Record using HTML Hyperlink JSP!!!
    By hassaanid2012 in forum JavaServer Pages (JSP) and JSTL
    Replies: 8
    Last Post: 01-09-2013, 03:58 PM
  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: 3
    Last Post: 03-26-2009, 07:35 AM
  4. how to delete record from db using jsp
    By Manas Das in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-31-2009, 07:36 PM
  5. Replies: 0
    Last Post: 07-31-2008, 10:44 AM

Posting Permissions

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