Posted on 2006-04-22
Last Modified: 2010-03-31
I have this line of code:
document = builder.parse(new File(argv[0]));

I want document to not read whitespaces or new lines. How can this be done?
Question by:no158
    LVL 14

    Expert Comment

    use java.util.Scanner
    it can parse primitive types
    LVL 30

    Accepted Solution

    >> I want document to not read whitespaces or new lines. How can this be done?

    Meaning new lines exist in the values contained inside the nodes? You can trim the values after reading using:

    String value = node.getNodeValue ().trim () ;

    - if that is what you are looking it.

    If the XML contains new-lines/ tabs between the nodes for formatting, like:


    - those new lines and tabs, etc will automatically be handled by the parser.
    >> it can parse primitive types

    I think its about parsing an XML document :) not primitives.

    Author Comment

    Mayankeagle, thanks for the tip with trim(). That is exactly what I was looking for.
    LVL 30

    Expert Comment

    You can also write your own, like:

        public static String trim ( String input )
            if ( input == null )
                return null ; // end if
            char[] array = input.trim ().toCharArray () ;        
            int endIndex = array.length - 1, startIndex ;
            for ( startIndex = 0 ; startIndex <= endIndex ; startIndex ++ )
                char c = array[startIndex] ;
                if ( c != '\0' && c != '\t' && c != '\r' && c != '\n' )
                    break ; // end if
            } // end for
            for ( ; endIndex >= startIndex ; endIndex -- )
                char c = array[endIndex] ;
                if ( c != '\0' && c != '\t' && c != '\r' && c != '\n' )
                    break ; // end if
            } // end for
            return new String ( array, startIndex, endIndex - startIndex + 1 ) ;
        } // end of trim ()
    LVL 86

    Expert Comment

    You would be better not to allow it (since you're not interested in it) to pollute the document in the first place by using a FilterReader

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now