This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes how to append nodes to the xml document Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "how to append nodes to the xml document" Watch "how to append nodes to the xml document" New topic
Author

how to append nodes to the xml document

kesava chaitanya
Ranch Hand

Joined: Aug 15, 2001
Posts: 140
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
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
What do you mean by "overwriting nodes"? Could you post the XML this code produces into the specified file?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
kesava chaitanya
Ranch Hand

Joined: Aug 15, 2001
Posts: 140
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: how to append nodes to the xml document
 
Similar Threads
how to use insertBefore method
problem while appending child nodes
how to remove spaces while updating xml and How to get indenting?
Help regarding how to check the element entered is equal or not in xml file
Java code to append data into a existing xml file