how to remove blank spaces in xml

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madhuriks
    New Member
    • Jun 2010
    • 149

    how to remove blank spaces in xml

    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

    Code:
      
    
    private 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())) {
                   
                    Node packNode = child.getParentNode().getParentNode();
                    Node packsNode = parent.getFirstChild();
                    packsNode.removeChild(packNode);
    
                    break;
                }
            }
    }
    thanks in advance,
    madhu.
  • madhuriks
    New Member
    • Jun 2010
    • 149

    #2
    hi,
    can anyone provide me the solution for the post i posted

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      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.

      Comment

      • madhuriks
        New Member
        • Jun 2010
        • 149

        #4
        hi gud mrng,
        my xml file is as follows

        xml file before deletion

        Code:
        <?xml version="1.0" encoding="UTF-8" standalone="no"?>
        <PACKS>
        <MA00>
        <GENERAL>
        <PACK_NAME>WAP_00</PACK_NAME>
        <ACT_STATUS>MA00</ACT_STATUS>
        <DEACT_STATUS>MD00</DEACT_STATUS>
        <ALLOW_POSTPAID>TYPE,2:</ALLOW_POSTPAID>
        <SHORT_CODE>1</SHORT_CODE>
        <ACT_KEYWORD>MOM</ACT_KEYWORD>
        <DEACT_KEYWORD>UMO</DEACT_KEYWORD>
        </GENERAL>
        </MA00>
        <MA01>
        <GENERAL>
        <PACK_NAME>WAP_01</PACK_NAME>
        <ACT_STATUS>MA01</ACT_STATUS>
        <DEACT_STATUS>MD01</DEACT_STATUS>
        <ALLOW_POSTPAID>TYPE,2:</ALLOW_POSTPAID>
        <SHORT_CODE>1</SHORT_CODE>
        <ACT_KEYWORD>MOM</ACT_KEYWORD>
        <DEACT_KEYWORD>UMO</DEACT_KEYWORD>
        </GENERAL>
        </MA01>
        <MA02>
        <GENERAL>
        <PACK_NAME>WAP_02</PACK_NAME>
        <ACT_STATUS>MA02</ACT_STATUS>
        <DEACT_STATUS>MD02</DEACT_STATUS>
        <ALLOW_POSTPAID>TYPE,2:</ALLOW_POSTPAID>
        <SHORT_CODE>1</SHORT_CODE>
        <ACT_KEYWORD>MOM</ACT_KEYWORD>
        <DEACT_KEYWORD>UMO</DEACT_KEYWORD>
        </GENERAL>
        </MA02>
        </PACKS>
        if i delete <MA01> then the o/p xml file is

        xml file after deletion

        Code:
        <?xml version="1.0" encoding="UTF-8" standalone="no"?>
        <PACKS>
        <MA00>
        <GENERAL>
        <PACK_NAME>WAP_00</PACK_NAME>
        <ACT_STATUS>MA00</ACT_STATUS>
        <DEACT_STATUS>MD00</DEACT_STATUS>
        <ALLOW_POSTPAID>TYPE,2:</ALLOW_POSTPAID>
        <SHORT_CODE>1</SHORT_CODE>
        <ACT_KEYWORD>MOM</ACT_KEYWORD>
        <DEACT_KEYWORD>UMO</DEACT_KEYWORD>
        </GENERAL>
        </MA00>
        
        <MA02>
        <GENERAL>
        <PACK_NAME>WAP_02</PACK_NAME>
        <ACT_STATUS>MA02</ACT_STATUS>
        <DEACT_STATUS>MD02</DEACT_STATUS>
        <ALLOW_POSTPAID>TYPE,2:</ALLOW_POSTPAID>
        <SHORT_CODE>1</SHORT_CODE>
        <ACT_KEYWORD>MOM</ACT_KEYWORD>
        <DEACT_KEYWORD>UMO</DEACT_KEYWORD>
        </GENERAL>
        </MA02>
        </PACKS>
        after every deletion it is providing one white space..can u check and tell me where the code gets wrong.

        thanks,
        madhu.

        Comment

        • madhuriks
          New Member
          • Jun 2010
          • 149

          #5
          need help regarding my post

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            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.

            Comment

            • madhuriks
              New Member
              • Jun 2010
              • 149

              #7
              hi gud evening,
              i had changed my code...what u suggested..even though im not getting..here is my modified code..

              Code:
              package proj;
              
              import java.io.*;
              import java.io.IOException;
              import java.io.PrintWriter;
              import java.util.Properties;
              import javax.servlet.*;
              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").trim();
              
                      try {
                          DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
                          builderFactory.setIgnoringElementContentWhitespace(true);
                          DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
                          
                          // Creating the path for XML File using Properties
                          ServletContext servletContextObj = request.getSession().getServletContext();
                          String excelTemplatesPath = servletContextObj.getRealPath("/").trim();
                          String filePaths = excelTemplatesPath + File.separator
                                  + "config.properties".trim();
                          Properties propObj = new Properties();
                          propObj.load(new FileInputStream(filePaths.trim()));
                          String gprsLoginPath = null;
                          gprsLoginPath = propObj.getProperty("GPRS_LOGIN_PATH").trim();
                          if (gprsLoginPath.trim() == null || gprsLoginPath.trim().equals("")) {
                              gprsLoginPath = excelTemplatesPath.trim();
                          }
                          gprsLoginPath += File.separator + "login.xml".trim();
                          java.io.File file = new java.io.File(gprsLoginPath.trim());
                          Document doc = null;
                          if (file.exists()) {
                              try {
                                  doc = docBuilder.parse(file);
                                  doc.normalizeDocument();
                              } catch (Exception ex) {
                                  doc = docBuilder.newDocument();
                                  doc.normalize();
                                  Element root = doc.createElement("PACKS");
                                  doc.appendChild(root);
                                  doc.normalize();
                              }
                          } else {
                              doc = docBuilder.newDocument();
                              doc.normalize();
                              // 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);
                          child.getTextContent().trim();
                          System.out.println("child " + child.getTextContent());
                          
                          if (child.getTextContent().trim() != null && child.getTextContent().trim().equalsIgnoreCase(filtervalue.trim())) {
              
                              // Node packNode = child.getParentNode();
                              Node packNode = child.getParentNode().getParentNode();
                              packNode.getTextContent().trim();
                              Node packsNode = parent.getFirstChild();
                              packsNode.setNodeValue(filtervalue.trim());
                              packsNode.removeChild(packNode);
                              packsNode.getTextContent().trim();
                  //          parent.removeChild(child.getParentNode().getParentNode());
                              break;
                          }
                      }
                      parent.getDoctype();
                      parent.getDocumentURI().trim();
                      parent.normalizeDocument();
                      parent.normalize();
                     // TransformerFactory instance is used to create Transformer objects.
                      TransformerFactory factory = TransformerFactory.newInstance();
                      Transformer transformer = factory.newTransformer();
                      transformer.setOutputProperty(OutputKeys.INDENT,"no");
                      // create string from xml tree
                      StringWriter sw = new StringWriter();
                      StreamResult result = new StreamResult(sw);
                      result.setOutputStream(null);
                     
                      DOMSource source = new DOMSource(parent);
                      transformer.transform(source, result);
                      String xmlString = sw.toString().trim();
                      xmlString.trim();
                      BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
                              new FileOutputStream(file)));
                      bw.write(xmlString.trim());
                      bw.flush();
                      bw.close();
                 }
              }
              can u suggest me where to change my code

              Comment

              • jkmyoung
                Recognized Expert Top Contributor
                • Mar 2006
                • 2057

                #8
                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.

                Comment

                • madhuriks
                  New Member
                  • Jun 2010
                  • 149

                  #9
                  which one packsNode or packNode one..i need to delete

                  Comment

                  • jkmyoung
                    Recognized Expert Top Contributor
                    • Mar 2006
                    • 2057

                    #10
                    ?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

                    Comment

                    Working...