469,366 Members | 2,198 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

how to remove blank spaces in xml

149 100+
hi,
i tried to delete an element in xml..for that i used servlet coding...it is getting deleted but blank spaces are remaining i dont want that blank spaces..can any one do me needfull..here is my code.. its urgent

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. private void delNode(java.io.File file, Document parent, String filtervalue)
  4.               throws Exception
  5.       {
  6.         NodeList nodeList = parent.getElementsByTagName("PACK_NAME");
  7.         for (int i = 0; i < nodeList.getLength(); i++) {
  8.             Node child = nodeList.item(i);
  9.             System.out.println("child " + child.getTextContent());
  10.             if (child.getTextContent() != null && child.getTextContent().trim().equalsIgnoreCase(filtervalue.trim())) {
  11.  
  12.                 Node packNode = child.getParentNode().getParentNode();
  13.                 Node packsNode = parent.getFirstChild();
  14.                 packsNode.removeChild(packNode);
  15.  
  16.                 break;
  17.             }
  18.         }
  19. }
  20.  
thanks in advance,
madhu.
Jul 22 '10 #1
9 8749
madhuriks
149 100+
hi,
can anyone provide me the solution for the post i posted
Jul 23 '10 #2
jkmyoung
2,057 Expert 2GB
Can you show an example of where this is occurring? The blank spaces don't seem to be inserted anywhere -> they probably pre-exist so you will have to remove those too.
Jul 29 '10 #3
madhuriks
149 100+
hi gud mrng,
my xml file is as follows

xml file before deletion

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <PACKS>
  3. <MA00>
  4. <GENERAL>
  5. <PACK_NAME>WAP_00</PACK_NAME>
  6. <ACT_STATUS>MA00</ACT_STATUS>
  7. <DEACT_STATUS>MD00</DEACT_STATUS>
  8. <ALLOW_POSTPAID>TYPE,2:</ALLOW_POSTPAID>
  9. <SHORT_CODE>1</SHORT_CODE>
  10. <ACT_KEYWORD>MOM</ACT_KEYWORD>
  11. <DEACT_KEYWORD>UMO</DEACT_KEYWORD>
  12. </GENERAL>
  13. </MA00>
  14. <MA01>
  15. <GENERAL>
  16. <PACK_NAME>WAP_01</PACK_NAME>
  17. <ACT_STATUS>MA01</ACT_STATUS>
  18. <DEACT_STATUS>MD01</DEACT_STATUS>
  19. <ALLOW_POSTPAID>TYPE,2:</ALLOW_POSTPAID>
  20. <SHORT_CODE>1</SHORT_CODE>
  21. <ACT_KEYWORD>MOM</ACT_KEYWORD>
  22. <DEACT_KEYWORD>UMO</DEACT_KEYWORD>
  23. </GENERAL>
  24. </MA01>
  25. <MA02>
  26. <GENERAL>
  27. <PACK_NAME>WAP_02</PACK_NAME>
  28. <ACT_STATUS>MA02</ACT_STATUS>
  29. <DEACT_STATUS>MD02</DEACT_STATUS>
  30. <ALLOW_POSTPAID>TYPE,2:</ALLOW_POSTPAID>
  31. <SHORT_CODE>1</SHORT_CODE>
  32. <ACT_KEYWORD>MOM</ACT_KEYWORD>
  33. <DEACT_KEYWORD>UMO</DEACT_KEYWORD>
  34. </GENERAL>
  35. </MA02>
  36. </PACKS>
  37.  
if i delete <MA01> then the o/p xml file is

xml file after deletion

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <PACKS>
  3. <MA00>
  4. <GENERAL>
  5. <PACK_NAME>WAP_00</PACK_NAME>
  6. <ACT_STATUS>MA00</ACT_STATUS>
  7. <DEACT_STATUS>MD00</DEACT_STATUS>
  8. <ALLOW_POSTPAID>TYPE,2:</ALLOW_POSTPAID>
  9. <SHORT_CODE>1</SHORT_CODE>
  10. <ACT_KEYWORD>MOM</ACT_KEYWORD>
  11. <DEACT_KEYWORD>UMO</DEACT_KEYWORD>
  12. </GENERAL>
  13. </MA00>
  14.  
  15. <MA02>
  16. <GENERAL>
  17. <PACK_NAME>WAP_02</PACK_NAME>
  18. <ACT_STATUS>MA02</ACT_STATUS>
  19. <DEACT_STATUS>MD02</DEACT_STATUS>
  20. <ALLOW_POSTPAID>TYPE,2:</ALLOW_POSTPAID>
  21. <SHORT_CODE>1</SHORT_CODE>
  22. <ACT_KEYWORD>MOM</ACT_KEYWORD>
  23. <DEACT_KEYWORD>UMO</DEACT_KEYWORD>
  24. </GENERAL>
  25. </MA02>
  26. </PACKS>
  27.  
after every deletion it is providing one white space..can u check and tell me where the code gets wrong.

thanks,
madhu.
Jul 30 '10 #4
madhuriks
149 100+
need help regarding my post
Jul 30 '10 #5
jkmyoung
2,057 Expert 2GB
It's a little hard to do it with the way you have your code set up. Before you removed packnode:

get children nodes from packsNode
Search for packnode in this list.
Get the node before and after pack node (in the child node list)
If trimmed, they are equal to "", then remove them from packsNode as well.
Jul 30 '10 #6
madhuriks
149 100+
hi gud evening,
i had changed my code...what u suggested..even though im not getting..here is my modified code..

Expand|Select|Wrap|Line Numbers
  1. package proj;
  2.  
  3. import java.io.*;
  4. import java.io.IOException;
  5. import java.io.PrintWriter;
  6. import java.util.Properties;
  7. import javax.servlet.*;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.http.HttpServlet;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12.  
  13. import org.w3c.dom.*;
  14. import javax.xml.parsers.*;
  15. import javax.xml.transform.*;
  16. import javax.xml.transform.dom.*;
  17. import javax.xml.transform.stream.*;
  18.  
  19. public class delete extends HttpServlet {
  20.  
  21.     public void doPost(HttpServletRequest request, HttpServletResponse response)
  22.             throws ServletException, IOException {
  23.  
  24.         response.setContentType("text/html");
  25.         PrintWriter pw = response.getWriter();
  26.         String packName = request.getParameter("packName").trim();
  27.  
  28.         try {
  29.             DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
  30.             builderFactory.setIgnoringElementContentWhitespace(true);
  31.             DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
  32.  
  33.             // Creating the path for XML File using Properties
  34.             ServletContext servletContextObj = request.getSession().getServletContext();
  35.             String excelTemplatesPath = servletContextObj.getRealPath("/").trim();
  36.             String filePaths = excelTemplatesPath + File.separator
  37.                     + "config.properties".trim();
  38.             Properties propObj = new Properties();
  39.             propObj.load(new FileInputStream(filePaths.trim()));
  40.             String gprsLoginPath = null;
  41.             gprsLoginPath = propObj.getProperty("GPRS_LOGIN_PATH").trim();
  42.             if (gprsLoginPath.trim() == null || gprsLoginPath.trim().equals("")) {
  43.                 gprsLoginPath = excelTemplatesPath.trim();
  44.             }
  45.             gprsLoginPath += File.separator + "login.xml".trim();
  46.             java.io.File file = new java.io.File(gprsLoginPath.trim());
  47.             Document doc = null;
  48.             if (file.exists()) {
  49.                 try {
  50.                     doc = docBuilder.parse(file);
  51.                     doc.normalizeDocument();
  52.                 } catch (Exception ex) {
  53.                     doc = docBuilder.newDocument();
  54.                     doc.normalize();
  55.                     Element root = doc.createElement("PACKS");
  56.                     doc.appendChild(root);
  57.                     doc.normalize();
  58.                 }
  59.             } else {
  60.                 doc = docBuilder.newDocument();
  61.                 doc.normalize();
  62.                 // Creating Root Tag
  63.                 Element root = doc.createElement("PACKS");
  64.                 doc.appendChild(root);
  65.             }
  66.  
  67.             delNode(file, doc, packName);
  68.             pw.println("<b>Xml File Deleted Successfully</b>");
  69.         } catch (Exception e) {
  70.             System.out.println(e);
  71.         }
  72.     }
  73.  
  74.     public static void delNode(java.io.File file, Document parent, String filtervalue)
  75.             throws Exception {
  76.         NodeList nodeList = parent.getElementsByTagName("PACK_NAME");
  77.        for (int i = 0; i < nodeList.getLength(); i++) {
  78.             Node child = nodeList.item(i);
  79.             child.getTextContent().trim();
  80.             System.out.println("child " + child.getTextContent());
  81.  
  82.             if (child.getTextContent().trim() != null && child.getTextContent().trim().equalsIgnoreCase(filtervalue.trim())) {
  83.  
  84.                 // Node packNode = child.getParentNode();
  85.                 Node packNode = child.getParentNode().getParentNode();
  86.                 packNode.getTextContent().trim();
  87.                 Node packsNode = parent.getFirstChild();
  88.                 packsNode.setNodeValue(filtervalue.trim());
  89.                 packsNode.removeChild(packNode);
  90.                 packsNode.getTextContent().trim();
  91.     //          parent.removeChild(child.getParentNode().getParentNode());
  92.                 break;
  93.             }
  94.         }
  95.         parent.getDoctype();
  96.         parent.getDocumentURI().trim();
  97.         parent.normalizeDocument();
  98.         parent.normalize();
  99.        // TransformerFactory instance is used to create Transformer objects.
  100.         TransformerFactory factory = TransformerFactory.newInstance();
  101.         Transformer transformer = factory.newTransformer();
  102.         transformer.setOutputProperty(OutputKeys.INDENT,"no");
  103.         // create string from xml tree
  104.         StringWriter sw = new StringWriter();
  105.         StreamResult result = new StreamResult(sw);
  106.         result.setOutputStream(null);
  107.  
  108.         DOMSource source = new DOMSource(parent);
  109.         transformer.transform(source, result);
  110.         String xmlString = sw.toString().trim();
  111.         xmlString.trim();
  112.         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
  113.                 new FileOutputStream(file)));
  114.         bw.write(xmlString.trim());
  115.         bw.flush();
  116.         bw.close();
  117.    }
  118. }
  119.  
can u suggest me where to change my code
Jul 30 '10 #7
jkmyoung
2,057 Expert 2GB
3 Mistakes.
1. Whoever gave you that code doesn't realize that the empty node is in the middle. That code will have no affect.

2. The second mistake they've made is thinking that trim() will actually do anything instead of just returning a string to nobody. The node would have to be set to the new value.

3. There are text (whitespace) nodes on the side of your packnode; the text is not specfically part of the packnode.
====
I realize I too have made a mistake. If you delete both text nodes, you will not have a hard return that you want. Delete one of them only.
Jul 30 '10 #8
madhuriks
149 100+
which one packsNode or packNode one..i need to delete
Jul 30 '10 #9
jkmyoung
2,057 Expert 2GB
?You do realize packsNode is the <PACKS> node. If you deleted this, you would be deleting the entire content of your document!

get the children nodes from packsNode
Search for packnode in this list.
Get the node after packnode (in the child node list) eg. if the index of packnode is i, the child node will be in index i+1 in the children nodes nodelist.
If it is a text node, and when trimmed is equal to "", then remove this node from packsNode as well
Jul 30 '10 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Nimmy | last post: by
3 posts views Thread by ShaeMills via AccessMonster.com | last post: by
1 post views Thread by abhilash12 | last post: by
KeredDrahcir
1 post views Thread by KeredDrahcir | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.