hi
when i run the
servlet program i want to append nodes to existing to nodes;but actually it is overwriting the nodes;what changes should i made to get desired output
package example;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.PrintWriter;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.*;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.StringWriter;
import java.io.IOException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
public class XMLController extends HttpServlet
{
private static final
String CONTENT_TYPE = "text/html; charset=windows-1252";
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
String fname="name.xml";
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = fac.newDocumentBuilder();
Document
doc = db.newDocument();
//-------------------------------
Element rt = doc.createElement("India");
Element main1 = doc.createElement("IndianPlayer");
Element productnames1 = doc.createElement("Name");
Text product1 = doc.createTextNode(request.getParameter("name1"));
Element price1 = doc.createElement("Age");
Text priceValues1 = doc.createTextNode(request.getParameter("age1"));
productnames1.appendChild(product1);
price1.appendChild(priceValues1);
main1.appendChild(productnames1);
main1.appendChild(price1);
rt.appendChild(main1);
doc.appendChild(rt);
saveDocAsFile(doc,fname);
}
catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
public static void saveDocAsFile(Document doc, String fname) {
try {
TransformerFactory tfFac = TransformerFactory.newInstance();
// use null trandformation
Transformer tf = tfFac.newTransformer();
tf.setOutputProperty(OutputKeys.INDENT,"yes");
tf.transform(new DOMSource(doc), new StreamResult(fname));
}
/* catch (IOException ioe) {
ioe.printStackTrace();
}*/
catch (TransformerException e) {
e.printStackTrace();
}
}
}
bye
chaitanya