Strip whitespace and newlines from XML in Java

后端 未结 6 2094
半阙折子戏
半阙折子戏 2020-12-01 21:01

Using Java, I would like to take a document in the following format:


 
    
 
         


        
6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 21:38

    Working solution following instructions in the question's comments by @Luiggi Mendoza.

    public static String trim(String input) {
        BufferedReader reader = new BufferedReader(new StringReader(input));
        StringBuffer result = new StringBuffer();
        try {
            String line;
            while ( (line = reader.readLine() ) != null)
                result.append(line.trim());
            return result.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    

提交回复
热议问题