如何删除XML

时间:2017-03-30 16:30:45

标签: java xml web-services soap java.util.scanner

我在下面有以下函数读取xml文件。但是,我需要删除一个标记和另一个标记之间的空格和换行符。此xml被发送到WebService SOAP,没有它返回相同的错误。我该怎么办?

public static String lerPasta() throws FileNotFoundException, InterruptedException{
      String texto = null;  

      int i = 0;      
        try{    
            FileFilter filter = new FileFilter() {
            public boolean accept(File file) {
                return file.getName().endsWith(".XML");
                }
            };
            File dir = new File(diretorioIn);
           // String texto = null;
            File[] files = dir.listFiles(filter);

            for(i = 0; i < files.length ; i++){
                if(files[i].length()==0){
                     System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+" lerPasta():NoSuchElementException:Arquivo vazio detectado. Sera ignorado momentaneamente :" + files[i].toString());

                }
                else{
                    Scanner s = new Scanner(new File(files[i].toString()), "UTF-8");
                    texto = s.useDelimiter("\\A").next();     
                    verificaTipoXML(files[i].toString().substring(diretorioIn.length()));
                    System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+" verificaTipoXML:files[i].toString(): "+files[i].toString());
                    System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+" lerPasta():" + files[i].toString());
                    System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+" verificaTipoXML():" + files[i].toString().substring(diretorioIn.length()+0));
                    deleta = files[i].toString();                  
                    deleta2 = files[i].toString().substring(diretorioIn.length());
                    deletar = files[i];
                    System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+" deleta:"+deleta2);
                    nomegerado = files[i].toString().substring(diretorioIn.length()+3);
                    System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+" GLOBAL:nomegerado:" + nomegerado);
                    s.close();

                }

             }

        }catch(FileNotFoundException e){
            System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+"lerPasta():ERRO: FileNotFoundException : Tentando novamente ...");
            //TimeUnit.SECONDS.sleep(2);
        }    
      //  catch(NoSuchElementException e){
      //      System.out.println(ZonedDateTime.now(fusoHorarioDeSaoPaulo)+"lerPasta():ERRO: NoSuchElementException : Tentando novamente ...");          
     //   }
        return texto;
    }

SOAP返回:

  

                            客户                              msg解析中的错误:在第5行解析SOAP有效内容时出现XML错误:无效   字符                                  

1 个答案:

答案 0 :(得分:0)

这是一种方法。

在类似帖子strip-whitespace-and-newlines-from-xml-in-java

上查看此答案