Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to append nodes to the xml document

 
kesava chaitanya
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • 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
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic