hi,
in my project i need to develop xml file..i created...im facing problem in deletion of white spaces in xml file..the deletion is working fine..but after deleting the elements it is showing white spaces..but i need to delete that white spaces...can anyone provide me how to do...here is my code...
package proj;
import java.io.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
public class delete extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String packName = request.getParameter("packName");
try {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
// Creating the path for XML File using Properties
ServletContext servletContextObj = request.getSession().getServletContext();
String excelTemplatesPath = servletContextObj.getRealPath("/");
String filePaths = excelTemplatesPath + File.separator
+ "config.properties";
Properties propObj = new Properties();
propObj.load(new FileInputStream(filePaths));
String gprsLoginPath = null;
gprsLoginPath = propObj.getProperty("GPRS_LOGIN_PATH");
if (gprsLoginPath == null || gprsLoginPath.trim().equals("")) {
gprsLoginPath = excelTemplatesPath;
}
gprsLoginPath += File.separator + "login.xml";
java.io.File file = new java.io.File(gprsLoginPath);
Document doc = null;
if (file.exists()) {
try {
doc = docBuilder.parse(file);
} catch (Exception ex) {
doc = docBuilder.newDocument();
Element root = doc.createElement("PACKS");
doc.appendChild(root);
}
} else {
doc = docBuilder.newDocument();
// Creating Root Tag
Element root = doc.createElement("PACKS");
doc.appendChild(root);
}
delNode(file, doc, packName);
pw.println("<b>Xml File Deleted Successfully</b>");
} catch (Exception e) {
System.out.println(e);
}
}
public static void delNode(java.io.File file, Document parent, String filtervalue)
throws Exception {
NodeList nodeList = parent.getElementsByTagName("PACK_NAME");
for (int i = 0; i < nodeList.getLength(); i++) {
Node child = nodeList.item(i);
System.out.println("child " + child.getTextContent());
if (child.getTextContent() != null && child.getTextContent().trim().equalsIgnoreCase(filtervalue.trim()) && child.getNodeType() == Node.TEXT_NODE) {
Node packNode = child.getParentNode().getParentNode();
Node packsNode = parent.getFirstChild();
packsNode.removeChild(packNode);
break;
}
}
parent.normalize();
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(parent);
transformer.transform(source, result);
String xmlString = sw.toString();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file)));
bw.write(xmlString);
bw.flush();
bw.close();
}
}
its urgent can anyone tell me how to do..waiting for the reply,
madhu.