473,834 Members | 1,358 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to remove blank spaces in xml

149 New Member
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 10030
madhuriks
149 New Member
hi,
can anyone provide me the solution for the post i posted
Jul 23 '10 #2
jkmyoung
2,057 Recognized Expert Top Contributor
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 New Member
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 New Member
need help regarding my post
Jul 30 '10 #5
jkmyoung
2,057 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Top Contributor
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 New Member
which one packsNode or packNode one..i need to delete
Jul 30 '10 #9
jkmyoung
2,057 Recognized Expert Top Contributor
?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

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

Similar topics

9
6697
by: Jean-Marc Molina | last post by:
Hello, I can't find a way to execute a Windows application, whose directory path contains blank spaces, from a PHP script. I also wonder if the problem happens under Linux and other OS. Working dir : "C:\test copy" "copy.php" PHP script : <?php
1
4089
by: Danny | last post by:
Given this: "*" the pattern to remove the htnl code between brackets, how can I remove a series of spaces and just make one space. like here is a series of blank spaces thanks
2
2711
by: Nimmy | last post by:
Hi, How can I print blank spaces with printf() I want something like printf("%6d %2d", lcVar1, lcVarb); Printout should be: 123123 34 12 spaces in between 123123 and 34 and 15 spaces at the end again,
3
8810
kamill
by: kamill | last post by:
how can i remove blank spaces from text file......is there any function to remove a perticular chr from file...
3
3779
by: ShaeMills via AccessMonster.com | last post by:
In my table I imported from .txt, one of my columns has blank spaces. The column is as follows, how do I eliminate the blank spaces in between the second and third, and fifth and sixth digits? 12 345 6789 78 241 5869 04 258 3695 14 254 1136 00 045 0285
1
1456
by: abhilash12 | last post by:
validating blank spaces. The input box should accept the value - for eg "JOHN SMITH" but it should not allow two blank spaces.
1
3330
by: maryanncanor | last post by:
Hi everyone, My problem is whenever I export a report to a textfile. The output textfile have blank spaces. Here is my query: SELECT ( & '|' & & '|' & ......................and so on .........................AS EXPR1 FROM ;
1
1764
KeredDrahcir
by: KeredDrahcir | last post by:
I have some values in an array and I need to remove the blank spaces before them. I've used array_walk($context_array, 'trim_value');To trim the spaces using the function: function trim_value(&$value) { $value = trim($value, "\xC2\xA0"); } This seems to have no effect. I need to convert my values into an array of single characters. I don't have PHP 5 so I've used a function to do the same job as str_split.
2
2197
by: SwapnilD | last post by:
I'm implementing a feature which reads comma separated txt file from server(one line at a time). Format of file is fixed, There are 3 columns on each row. After reading the row from file I insert it in a database table. But first value of each row aoutomatically appends two blank spaces, even if there are no blank spaces at the begining of each row. I tried to use "TRIM/LTRIM/Replace" to remove blank spece from the first value of...
0
9643
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10505
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10214
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7755
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6952
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5624
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5790
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.