By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
463,061 Members | 477 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 463,061 IT Pros & Developers. It's quick & easy.

how to delete blank lines from xml file by using java

P: 34
Hi,

I wanted to delete a line from xml file which i did it. But after deletion of that line there is a blank space. Again if i am adding another line by using java that blank line remains as usual.

My xml file is :::::::

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<config>
<employee id = "1" name = "aa" />
<employee id = "2" name = "bb" />
<employee id = "3" name = "cc" />
</config>

For deletion of a line say employee having id = 2 i wrote the following java code:::::
Expand|Select|Wrap|Line Numbers
  1.  
  2.         TransformerFactory transformerFactory = null;
  3.     Transformer transformer = null;
  4.     File file = new File("/src/java/config.xml");
  5.         int employeeNodeLength = 0;
  6.         int idValue = 0;
  7.         try {
  8.         Element employee_elem= null;
  9.         DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
  10.         DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
  11.         Document document = documentBuilder.parse(new File("/src/java/config.xml"));
  12.                 NodeList list = document.getElementsByTagName("employee");
  13.         employeeNodeLength = list.getLength();
  14.  
  15.                 for( int i = 0; i < employeeNodeLength ; i++) {
  16.             Node employeeNode= document.getElementsByTagName("employee").item(i);
  17.             NamedNodeMap attributesList = employeeNode.getAttributes();
  18.                     Node employeeId = attributesList.getNamedItem("id");
  19.                     idValue = Integer.parseInt(employeeId.getTextContent());
  20.                     if(idValue == 2) {
  21.                 System.out.println("This "+employeeId.getTextContent()+" node is going to be deleted");
  22.                 employee_elem = (Element)document.getElementsByTagName("employee").item(i);
  23.                 // Remove the node
  24.                 employee_elem.getParentNode().removeChild(employee_elem);
  25.                 // Normalize the DOM tree to combine all adjacent nodes
  26.                 document.normalize();
  27.                 transformerFactory = TransformerFactory.newInstance();
  28.                 transformer = transformerFactory.newTransformer();
  29.                 transformer.setOutputProperty(OutputKeys.INDENT,"no");
  30.                 DOMSource source = new DOMSource(document);
  31.                 StreamResult result =  new StreamResult(file);
  32.                 transformer.transform(source, result);
  33.                 break;
  34.             }
  35.           }
  36.       } catch(Exception e) {
  37.           e.printStackTrace();
  38.       }
  39.  
  40.  
but after deletion of the line id = 2 there is a blank space between two lines employee id =1 and id = 3

I want that after removing one line/lines there should not be any blank space, it should be properly aligned

Thanks in advance
Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies

P: 3
For one, I am not sure why you want to remove the blank lines. The XML parsers will work irrespective of whether there is a new line or not.

If you still intend to "Format" the XML (i.e. indent the XML and remove blank lines), why would you want it to do XML way ? Why not normal String processing ?

Would suggest using Python (Jython) if you intend to just format the XML content.
Jun 28 '08 #2

Expert 100+
P: 785
For one, I am not sure why you want to remove the blank lines. The XML parsers will work irrespective of whether there is a new line or not.

If you still intend to "Format" the XML (i.e. indent the XML and remove blank lines), why would you want it to do XML way ? Why not normal String processing ?

Would suggest using Python (Jython) if you intend to just format the XML content.
How to do it with string processing:
a) load the file into a string s
b) use regular expression: s.replace("\\s+\\n", "\\n");
c) save string s into the old file back.
Jun 30 '08 #3

P: 34
How to do it with string processing:
a) load the file into a string s
b) use regular expression: s.replace("\\s+\\n", "\\n");
c) save string s into the old file back.
Thanks for your reply.
I know i can delete blank lines by loading file into a string but what i wanted is that if there is any xml parser method through which i can delete.

Any way thanks.
Jun 30 '08 #4

Expert 100+
P: 785
Thanks for your reply.
I know i can delete blank lines by loading file into a string but what i wanted is that if there is any xml parser method through which i can delete.

Any way thanks.
ok.
I did not try your program, but from my experience I can tell that a blank line is an empty node that you should remove, too.
Try to get the number of nodes for the parent. If you have more than you have lines of employees, then you probably have additional blank lines which also count as elements. That means:
employee1, blank, employee2, blank, employee3 and so on.

As I said, I have not yet tried your code, I am just guessing.
But I concluded from my experience in Javascript when building up the DOM structure from XML in Ajax for different web browsers: One browser makes these addditional blank elements, the other not.
Jun 30 '08 #5

Post your reply

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