Strip whitespace and newlines from XML in Java

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

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


 
    
 
         


        
6条回答
  •  抹茶落季
    2020-12-01 21:35

    Try this code. read and write methods in FileStream ignore whitespace and indents.

    try {
        File f1 = new File("source.xml");
        File f2 = new File("destination.xml");
        InputStream in = new FileInputStream(f1);  
        OutputStream out = new FileOutputStream(f2);
    
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0){
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
    System.out.println("File copied.");
    } catch(FileNotFoundException ex){
        System.out.println(ex.getMessage() + " in the specified directory.");
        System.exit(0);
    } catch(IOException e7){
        System.out.println(e7.getMessage());  
    }
    

提交回复
热议问题