how to delete blank lines from xml file by using java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BibhuAshish
    New Member
    • Oct 2007
    • 34

    #1

    how to delete blank lines from xml file by using java

    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:::::
    [CODE=Java]

    TransformerFact ory transformerFact ory = null;
    Transformer transformer = null;
    File file = new File("/src/java/config.xml");
    int employeeNodeLen gth = 0;
    int idValue = 0;
    try {
    Element employee_elem= null;
    DocumentBuilder Factory documentBuilder Factory = DocumentBuilder Factory.newInst ance();
    DocumentBuilder documentBuilder = documentBuilder Factory.newDocu mentBuilder();
    Document document = documentBuilder .parse(new File("/src/java/config.xml"));
    NodeList list = document.getEle mentsByTagName( "employee") ;
    employeeNodeLen gth = list.getLength( );

    for( int i = 0; i < employeeNodeLen gth ; i++) {
    Node employeeNode= document.getEle mentsByTagName( "employee").ite m(i);
    NamedNodeMap attributesList = employeeNode.ge tAttributes();
    Node employeeId = attributesList. getNamedItem("i d");
    idValue = Integer.parseIn t(employeeId.ge tTextContent()) ;
    if(idValue == 2) {
    System.out.prin tln("This "+employeeId.ge tTextContent()+ " node is going to be deleted");
    employee_elem = (Element)docume nt.getElementsB yTagName("emplo yee").item(i);
    // Remove the node
    employee_elem.g etParentNode(). removeChild(emp loyee_elem);
    // Normalize the DOM tree to combine all adjacent nodes
    document.normal ize();
    transformerFact ory = TransformerFact ory.newInstance ();
    transformer = transformerFact ory.newTransfor mer();
    transformer.set OutputProperty( OutputKeys.INDE NT,"no");
    DOMSource source = new DOMSource(docum ent);
    StreamResult result = new StreamResult(fi le);
    transformer.tra nsform(source, result);
    break;
    }
    }
    } catch(Exception e) {
    e.printStackTra ce();
    }

    [/CODE]

    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
  • venukb
    New Member
    • Jun 2008
    • 3

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

    Comment

    • chaarmann
      Recognized Expert Contributor
      • Nov 2007
      • 785

      #3
      Originally posted by venukb
      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.

      Comment

      • BibhuAshish
        New Member
        • Oct 2007
        • 34

        #4
        Originally posted by chaarmann
        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.

        Comment

        • chaarmann
          Recognized Expert Contributor
          • Nov 2007
          • 785

          #5
          Originally posted by BibhuAshish
          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.

          Comment

          Working...