Appending data to xml file


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Appending data to xml file

  1. #1
    Join Date
    Sep 2005
    Posts
    22

    Appending data to xml file

    Hi

    I have an xml file.I have to write all the details I enterd through UI.Suppose I have UserId & UserName. In the XML it should come like this.
    <Userdetails>
    <user>
    <userid>1<userid>
    <username>aaa</username>
    </user>
    <user>
    <userid>2<userid>
    <username>ssss</username>
    </user>
    <user>
    <userid>3<userid>
    <username>asdfd</username>
    </user>
    </Userdetails>

    I wrote the code like this.But In the XML file.it is overwriting the details.
    How to append the details in xml file.I am using JAXP.

    <CODE>
    public class DataBaseActivity {
    private static String User_Details = "Userdetails";
    private static String Main = "user";

    private static String UserId = "userId";

    private static String UserName = "userName";

    public void create_details(String userid,String userName) {

    System.out.println("UserId " + userid);
    System.out.println("userName " + userName);
    OutputStream outStream;
    try {
    outStream = new FileOutputStream("user.xml");


    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder;

    docBuilder = docBuilderFactory.newDocumentBuilder();

    Document document = docBuilder.newDocument();

    Element rootElement = document.createElement(User_Details);
    document.appendChild(rootElement);

    Element childroot = document.createElement(Main);
    rootElement.appendChild(childroot);

    Element em = document.createElement(UserId);
    em.appendChild(document.createTextNode(userid));
    childroot.appendChild(em);

    em = document.createElement(UserName);
    em.appendChild(document.createTextNode(userName));
    childroot.appendChild(em);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer;

    transformer = transformerFactory.newTransformer();

    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(outStream);

    transformer.transform(source, result);

    outStream.flush();
    outStream.close();
    }

    catch (IOException e)
    {
    e.printStackTrace();

    }catch (ParserConfigurationException e) {

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

    e.printStackTrace();
    }

    }


    }
    </CODE>

    it is overwriting the exixting details.It is comming as like this.
    <Userdetails>
    <user>
    <userid>1<userid>
    <username>aaa</username>
    </user>
    </Userdetails>

    it will overwrite the details as
    <Userdetails>
    <user>
    <userid>2<userid>
    <username>dfcdf</username>
    </user>
    </Userdetails>
    Can u tell me what to do

  2. #2
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    the method create_details() creates a new xml document on each invokation with the given data. so it#s no wonder that you get a xml document which only contains the data you gave to it. to append multiple datas to your xml document, you have to reuse the document object.
    make it like this (only a suggestion, I had no time to test or write proper code...):
    Code:
    public class myClass{
    
    private Document myDoc;
    private Element rootElement;
    
    public void createDocument(){
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder;
    
    docBuilder = docBuilderFactory.newDocumentBuilder();
    
    myDoc = docBuilder.newDocument();
    
    rootElement = document.createElement(User_Details);
    document.appendChild(rootElement);
    }
    
    public void addUserData(String userid,String userName){
    Element childroot = myDoc.createElement(Main);
    rootElement.appendChild(childroot);
    
    Element em = myDoc.createElement(UserId);
    em.appendChild(myDoc.createTextNode(userid));
    childroot.appendChild(em);
    
    em = myDoc.createElement(UserName);
    em.appendChild(myDoc.createTextNode(userName));
    childroot.appendChild(em);
    }
    
    public Document getDocument(){
    return myDoc;
    }
    
    public static void main(String [] argv){
      myClass mine = new myClass();
      mine.crrateDocument();
      mine.addData("1", "user1");
      mine.addData("2", "user2");
      OutputStream outStream = new FileOutputStream("user.xml");
      TransformerFactory transformerFactory = TransformerFactory.newInstance();
      Transformer transformer;
    
      transformer = transformerFactory.newTransformer();
    
      DOMSource source = new DOMSource(mine.getDocument());
      StreamResult result = new StreamResult(outStream);
    
      transformer.transform(source, result);
    
      outStream.flush();
      outStream.close();
    }
    }

  3. #3
    Join Date
    Sep 2005
    Posts
    22
    Hi

    Thanks.I got it worked...

Similar Threads

  1. web service to store data in XML file
    By raewyn in forum XML
    Replies: 1
    Last Post: 11-28-2005, 04:07 PM
  2. dynamic XML data island
    By Vincent in forum Web
    Replies: 1
    Last Post: 02-05-2003, 09:03 AM
  3. File uploading / objFile.Write problem
    By Tomer Cagan in forum ASP.NET
    Replies: 1
    Last Post: 07-24-2001, 09:01 AM
  4. Ado using Data Link File Question
    By KENHOW in forum VB Classic
    Replies: 0
    Last Post: 07-13-2001, 04:02 PM
  5. Replies: 0
    Last Post: 06-19-2001, 07:56 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center