• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java code to append data into a existing xml file

 
Ravi Doddi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai......
I have a jsp page in which there are some text fields. When I submit it, it goes to a servlet and I creates an xml file. So whenever I submit a new xml file is created, I need my previous data also present in it. Can anyone please help me, so that I can append data to an existing xml file.
Thankyou.
 
Paul Clapham
Sheriff
Pie
Posts: 20734
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't append data to an XML document. Doing that would cause it to be not well-formed. That's because an XML document must have a single root element.

So your requirement to "append data into" an existing XML document must mean that you are adding an element (or maybe more than one) as the last child of the root element. That's how you should write your code. And really, your requirements should have been specific and said that in the first place.
 
Ravi Doddi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Clapham:
You don't append data to an XML document. Doing that would cause it to be not well-formed. That's because an XML document must have a single root element.

So your requirement to "append data into" an existing XML document must mean that you are adding an element (or maybe more than one) as the last child of the root element. That's how you should write your code. And really, your requirements should have been specific and said that in the first place.



Thanks for your reply.
I'll make you more clear what I want.
If this is my xml file
<books>
<person>
<first>Tom</first>
<last>Cruise</last>
<age>45</age>
</person>
</books>

Now when I append some data, I should have my previous data in it. Like
<books>
<person>
<first>Tom</first>
<last>Cruise</last>
<age>45</age>
</person>
<person>
<first>Tom</first>
<last>Hanks</last>
<age>48</age>
</person>
</books>

This is how I want. But my data gets overwritten with previous data and I show up as
<books>
<person>
<first>Tom</first>
<last>Hanks</last>
<age>48</age>
</person>
</books>

So could you please suggest me some code. Thankyou
 
Paul Clapham
Sheriff
Pie
Posts: 20734
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, that's what I expected. So read your document into memory, create a new element, and add that new element as the last child of the document's root. Then write the document out again. DOM is a reasonably good technology for that.
 
Ravi Doddi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what my code is

first=doc.createElement("first");
first.appendChild(doc.createTextNode(firstName));
person.appendChild(first);

last=doc.createElement("last");
last.appendChild(doc.createTextNode(lastName));
person.appendChild(last);

age=doc.createElement("age");
age.appendChild(doc.createTextNode(ageYears));
person.appendChild(age);

books.appendChild(person);
doc.appendChild(books);

I dont understand how do you append with this.
 
deepika baghla
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey i am trying to overwrite values of elements in XML file. i am completely new and naive to access XML from java.
so can you please send the complete java code of which you have given snippet here
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really need to work through a good xml tutorial.

Sun's seems to have gotten outrageously complicated so its a good thing that Harold's book is now available on line.

Bill
 
deepika baghla
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have gone through that site. but what i want is to overwrite data in XML file instead of overwriting entire xml file.


here in the file, data value for tag <first> i.e. "Kiran" should be overwritten by new value say "ram" and same for other data also.
please help .. I am stuck here
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are stuck it is because you are not thinking about the problem correctly.
what i want is to overwrite data in XML file instead of overwriting entire xml file.


You can't treat an XML file like a random access file of records that you can write bits into. You have to either:

1. using DOM methods read the whole XML document file into an in memory DOM object, modify it, write the whole document out to a file (over the old one or a new one)

2. using SAX methods, dynamically read the document, writing a new file as you go until your program recognizes where the new data should go, insert the new XML formatted data, then continue reading and writing.
Emphatically not recommended for beginners.

Bill
 
jaytantra kumar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



You can use the following code for Appending the data in any xml file using Servlets:


package mqtest;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class XmlServlet extends HttpServlet{
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException{

/* String hostname="";
String hostip="";
String qmname="";
String hostport="";
String channel="";

if(request.getParameter("hostname")!=null
&& request.getParameter("hostip")!=null
&& request.getParameter("qmname")!=null
&& request.getParameter("hostport")!=null
&& request.getParameter("channel")!=null)
{
hostname = request.getParameter("hostname").toString();
hostip = request.getParameter("hostip").toString();
qmname = request.getParameter("qmname").toString();
hostport = request.getParameter("hostport").toString();
channel = request.getParameter("channel").toString();
}*/
String hostname = request.getParameter("hostname").toString();
String hostip = request.getParameter("hostip").toString();
String qmname = request.getParameter("qmname").toString();
String hostport = request.getParameter("hostport").toString();
String channel = request.getParameter("channel").toString();
try
{
System.out.println(hostname);
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();



createXmlTree(hostname,hostip,qmname,hostport,channel);


System.out.println("<b>Xml File Created Successfully</b>");
}
catch(Exception e)
{
System.out.println(e);
}
}
public void createXmlTree(String hostname,String hostip,String qmname,String hostport,String channel) throws Exception {
Element root;
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
File file = new File("workspace_jay\\MyRighttPro\\properties.xml");
if (file.exists())
{
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = fact.newDocumentBuilder();
doc = docBuilder.parse(file);
root = doc.getDocumentElement();
String sr = root.getNodeName();


//root = node.getNodeName();
}
else
{

//System.out.println(hostdetails);
root = doc.createElement("hostinformation");
doc.appendChild(root);
}

Element child = doc.createElement("hostdetails");
root.appendChild(child);

Element child1 = doc.createElement("hostname");
child.appendChild(child1);

Text text1 = doc.createTextNode(hostname);
child1.appendChild(text1);

Element child2 = doc.createElement("hostip");
child.appendChild(child2);

Text text2 = doc.createTextNode(hostip);
child2.appendChild(text2);

Element child3 = doc.createElement("qmname");
child.appendChild(child3);

Text text3 = doc.createTextNode(qmname);
child3.appendChild(text3);

Element child4 = doc.createElement("hostport");
child.appendChild(child4);

Text text4 = doc.createTextNode(hostport);
child4.appendChild(text4);

Element child5 = doc.createElement("channel");
child.appendChild(child5);

Text text5 = doc.createTextNode(channel);
child4.appendChild(text5);

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();


}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic