• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

how to append nodes to the xml document

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by "overwriting nodes"? Could you post the XML this code produces into the specified file?
 
kesava chaitanya
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when i run the program first time i will get the o/p as

<?xml version = '1.0'?>
<India>
<IndianPlayer>
<Name>111111111</Name>
<Age>111111111</Age>
</IndianPlayer>
</India>

when i run the program next time with different values

i want o/p as

<?xml version = '1.0'?>
<India>
<IndianPlayer>
<Name>111111111</Name>
<Age>111111111</Age>
</IndianPlayer>
<IndianPlayer>
<Name>22222</Name>
<Age>22222</Age>
</IndianPlayer>

</India>


it is creating xml file again o/p is coming as

<?xml version = '1.0'?>
<India><IndianPlayer>
<Name>22222</Name>
<Age>22222</Age>
</IndianPlayer>

</India>
[ June 15, 2004: Message edited by: kesava chaitanya ]
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. You're overwriting the file and you want to "update" the file. The solution is to read the XML file into a Document object before you start appending stuff into it.
 
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic