private static void addLinesToXML(File inFile,Iterable<String> lines) throws IOException {
  StringBuilder sb=new StringBuilder();
  sb.append("$1");
  for (  String l : lines) {
    sb.append("\n" + l);
  }
  String content=contents(inFile);
  content=content.replaceAll("(<\\?xml version.*\\?>)",sb.toString());
  FileOutputStream fos=new FileOutputStream(inFile);
  PrintWriter out=new PrintWriter(fos);
  out.print(content);
  out.flush();
  out.close();
}

private static void addLinesToXML ( File inFile , Iterable < String > lines ) throws IOException { StringBuilder sb = new StringBuilder ( ) ; sb . append ( "$1" ) ; for ( String l : lines ) { sb . append ( "\n" + l ) ; } String content = contents ( inFile ) ; content = content . replaceAll ( "(<\\?xml version.*\\?>)" , sb . toString ( ) ) ; FileOutputStream fos = new FileOutputStream ( inFile ) ; PrintWriter out = new PrintWriter ( fos ) ; out . print ( content ) ; out . flush ( ) ; out . close ( ) ; }
add some lines to a given xml file . the lines will be inserted directly below the first line in the xml . the first line has to be a " xml version " tag .
