Results 1 to 3 of 3
  1. #1
    phaniakella is offline Member
    Join Date
    May 2012
    Posts
    1
    Rep Power
    0

    Default XML file (DOM Parser) getting created with only one record

    Dear Experts

    I am new to Java and DOM Parsers. I am trying to create XML file as show below with 3 records. But after executing the below java code xml is getting generated with only last record. I wand all the 3 records in the XML. Please advise.

    Thanks
    Phani Akella

    Output:-

    <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
    - <BookStore>
    - <Books>
    <Title>Thinking in Java</Title>
    <Author>Bruce Eckel</Author>
    <Year>2014</Year>
    <Price>5000</Price>
    </Books>
    </BookStore>


    Java Code:-

    import java.io.File;
    import java.io.FileWriter;
    import javax.xml.*;
    import javax.xml.parsers.*;
    import javax.xml.transform.Result;
    import javax.xml.transform.Source;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    import org.w3c.dom.*;


    /**
    *
    * @author Phani
    */
    public class CreateXML {

    public static void main(String[] arg){

    try{
    DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
    DocumentBuilder db=dbf.newDocumentBuilder();
    Document doc=db.newDocument();

    Element root=doc.createElement("BookStore");
    doc.appendChild(root);

    Element Books=doc.createElement("Books");
    root.appendChild(Books);

    Element Title=doc.createElement("Title");
    Books.appendChild(Title);

    Element Author=doc.createElement("Author");
    Books.appendChild(Author);

    Element Year=doc.createElement("Year");
    Books.appendChild(Year);

    Element Price=doc.createElement("Price");
    Books.appendChild(Price);


    Title.setTextContent("Thinking in Java");
    Author.setTextContent("Bruce Eckel");
    Year.setTextContent("2012");
    Price.setTextContent("5000");

    Title.setTextContent("Thinking in Java");
    Author.setTextContent("Bruce Eckel");
    Year.setTextContent("2013");
    Price.setTextContent("5000");

    Title.setTextContent("Thinking in Java");
    Author.setTextContent("Bruce Eckel");
    Year.setTextContent("2014");
    Price.setTextContent("5000");

    File f1=new File("D:\\Dom_Book.xml");


    TransformerFactory tf=TransformerFactory.newInstance();
    Transformer t=tf.newTransformer();


    Source src=new DOMSource(doc);
    Result tgt=new StreamResult(System.out);
    Result tgt1=new StreamResult(f1);


    t.transform(src, tgt);
    t.transform(src, tgt1);


    }catch(Exception e){
    e.printStackTrace();
    }


    }

    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: XML file (DOM Parser) getting created with only one record

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

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: XML file (DOM Parser) getting created with only one record

    You're only creating a single new entry in the DOM.
    You are simply overwriting the data in that entry several times.

    And please read Darryls links if you are planning on posting more code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 7
    Last Post: 02-29-2012, 12:06 AM
  2. Replies: 9
    Last Post: 07-09-2011, 07:15 PM
  3. running the created jar file
    By Mostafa_mech in forum NetBeans
    Replies: 12
    Last Post: 07-02-2010, 11:06 AM
  4. pointing to 1st record in a file
    By swati.jyoti in forum New To Java
    Replies: 1
    Last Post: 04-25-2009, 08:38 AM
  5. How to find file created date.....
    By roshithmca in forum Advanced Java
    Replies: 1
    Last Post: 02-18-2008, 09:48 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
  •