Posted on 2006-04-22
Medium Priority
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

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

Accepted Solution

Mayank S earned 1000 total points
ID: 16515400
>> 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

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

Expert Comment

by:Mayank S
ID: 16515489
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

ID: 16515902
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

809 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