How to add to an xml document instead of over writing it

xml FileOutputStream , xml , writing xml , appending xml , creating xml file London, United Kingdom
  • 11 years ago

    Hello,

    How to add to an xml document instead of over writing it:

    Any help will be much appreciated...thanks in advance.

    I have managed to get my code to write an xml file with data from input fields from a jsp page... Now I actually need to add new entered details on the jsp page to the existing xml file instead of rewriting it everytime My sample code which currently rewrite the xml file is as follow bellow:

                public void addContact(String name, String number) {
    
                    	System.out.println("New Contact's Name and Number are '" + name
                    			+ "' and '" + number + "' respectively.!!!");
    
                    	//to write...Xml writer code
    
                    	System.out.println("got here start of write Xml***");
    
                    	//this method gets called but only rewrites the Xml file
                    	//Really need it to append/add to the Xml file
    
                    	try {
                    		String filename = getClass().getClassLoader().getResource("contact-db.xml").getPath();
                    		FileOutputStream fos = new FileOutputStream(filename);
                    		fos.write("<?xml version = \"1.0\"?>".getBytes());
                    		String s = "";
    
                    		s += "<contacts>";
                    		s += "<contact>";
                    		s += "<name>";
                    		s += name;
                    		s += "</name>";
                    		s += "<number>";
                    		s += number;
                    		s += "</number>";
                    		s += "</contact>";
                    		s += "</contacts>";
    
                    		s += "";
    
    
    
                    		fos.write(s.getBytes("utf-8"));
                    		fos.close();
    
                    		//check to see this method got called or not?
    
                    		System.out.println("***got here end of write Xml");
    
                    	} catch (FileNotFoundException e) {
                    		// TODO Auto-generated catch block
                    		e.printStackTrace();
                    	} catch (UnsupportedEncodingException e) {
                    		// TODO Auto-generated catch block
                    		e.printStackTrace();
                    	} catch (IOException e) {
                    		// TODO Auto-generated catch block
                    		e.printStackTrace();
                    	}
    
    
                    }
    

    Thanks you... newGraduate..

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“If Java had true garbage collection, most programs would delete themselves upon execution.” - Robert Sewell