2011-07-06 5 views
0

で出力を表示します。はこのjavaファイルを修正するには、私を助け、正しいフォーマットで出力を印刷してください、</p> <p>を私は次の形式で出力を表示するためのJavaコードを書くことで助けを必要と表示されたxml形式

<?xml version="1.0" encoding="UTF-8"?> 
<CBB> 
<CSV CKK="Folder_1"> 
    <SV_File CKK="File_1"> 
     <SV CKK="Line_1"/> 
     <SV CKK="Line_2"/> 
     <SV CKK="Line_3"/> 
     <SV CKK="Line_4"/> 
     <SV CKK="Line_5"/> 
     <SV CKK="Line_6"/> 
     <SV CKK="Line_7"/> 
     <SV_Depend DependentVariableID="File_1" SourceVariableID="Line_1"/> 
     <SV_Depend DependentVariableID="File_1" SourceVariableID="Line_2"/> 
     <SV_Depend DependentVariableID="File_1" SourceVariableID="Line_3"/> 
     <SV_Depend DependentVariableID="File_1" SourceVariableID="Line_4"/> 
     <SV_Depend DependentVariableID="File_1" SourceVariableID="Line_5"/> 
     <SV_Depend DependentVariableID="File_1" SourceVariableID="Line_6"/> 
     <SV_Depend DependentVariableID="File_1" SourceVariableID="Line_7"/> 
    </SV_File> 
    <SV_File CKK="File_2"> 
     <SV CKK="Line_1"/> 
     <SV CKK="Line_2"/> 
     <SV CKK="Line_3"/> 
     <SV CKK="Line_4"/> 
     <SV CKK="Line_5"/> 
     <SV CKK="Line_6"/> 
     <SV CKK="Line_7"/> 
     <SV_Depend DependentVariableID="File_2" SourceVariableID="Line_1"/> 
     <SV_Depend DependentVariableID="File_2" SourceVariableID="Line_2"/> 
     <SV_Depend DependentVariableID="File_2" SourceVariableID="Line_3"/> 
     <SV_Depend DependentVariableID="File_2" SourceVariableID="Line_4"/> 
     <SV_Depend DependentVariableID="File_2" SourceVariableID="Line_5"/> 
     <SV_Depend DependentVariableID="File_2" SourceVariableID="Line_6"/> 
     <SV_Depend DependentVariableID="File_2" SourceVariableID="Line_7"/> 
    </SV_File> 
</CSV> 

私はフォルダ(ABCD)内の2つのフォルダ(Folder_1とFolder_2)を持っています。 Folder_1の中には、2つのファイル "File_1"と "FIle_2"があります。

インサイド "file_1"、私はコードの7行を持ってliek LINE_1、Line_2 ... Line_7 ...

私は多くの場所で終了タグを見つけています。私はそれがFolder_1だけを終了するときに閉じられる必要があります。

import java.io.*; 
import java.util.*; 
import java.lang.String; 

public class Vardeps { 
public static void main(String args[]) throws FileNotFoundException 
{ 
    // File Writer for the XML 
    FileWriter fostream; 
    PrintWriter out = null;  
    String strOutputPath = "D:\\Current_\\"; 
    String strFilePrefix = "Temp"; 
    String filesListTrim; 
    int intID = 1; 

    // Read the file name for the SodaSystem 
    File startingDirectory= new File("D:\\Current_\\Additional_Files\\abcd"); 
    List<File> filesList = FileListing.getFileListing(startingDirectory); 
    File file = new File("D:\\Current_\\Additional_Files\\abcd"); 

    File[] files = file.listFiles(); 
    String[] children = file.list(); 


    // Try for the SodaSystem. 
    try{ 
    if(children == null) 
    { 
     // no files exist. 
     System.out.println("no files exist"); 
    } 
    else 
    { 
     // For the SodaSystem 
     fostream = new FileWriter(strOutputPath+"\\"+strFilePrefix+".xml"); 
     out = new PrintWriter(new BufferedWriter(fostream)); 


     out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 
     out.write("<CBB>"); 

     // Printing out the filenames for the SodaSystem 
     for(File fileName : filesList) 
     { 


      if (fileName.isDirectory() && fileName.getName().endsWith("vardeps")) 
      { 
       out.write("\t<CSV CommonKey = " + "\"" + fileName.getName().substring(0, 3) + "\"" + ">\n"); 

      } 
      else 
      { 
       out.println("\t<SV_File CommonKey = " + "\"" + fileName.getName().substring(0, (fileName.getName().length() - 4)) + "\"" + ">\n"); 


       // This holds the filename for the Relation CommonKey 
       filesListTrim = fileName.getName().substring(0, (fileName.getName().length() - 4)); 

       // Open the file that is the first 
       // command line parameter 
        FileInputStream fstream = new FileInputStream(fileName); 

        // Get the object of DataInputStream 
        DataInputStream in = new DataInputStream(fstream); 
        BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
        String strLine; 

        //Read File Line By Line 
        while ((strLine = br.readLine()) != null) 
        { 

         // Print the SodaVariable Entity. 
         out.println("\t<SV CommonKey = " + "\"" + strLine + "\"" + "/>"); 



         // Print the SodaVariableDependency RelationTable. 

         out.println("\t<SV_Depend DependentVariableID = " + "\"" + filesListTrim + "\" SourceVariableID = " + "\"" + strLine + "\"" + "/>"); 

         intID ++; 
         } 
        out.write("\n\t</SV>\n"); 
         //Close the input stream 
         in.close();      
      } 
      //out.println("/>"); 
      intID ++; 
      out.write("\n\t</SodaSystem>\n"); 
     } 
    } 

     out.println("\n</CBB>"); 
     out.flush();    
     out.close(); 
    } 
    catch(Exception E) { 
     E.printStackTrace(); 
    } 
} 
} 

問題は直面しています:フォーマットが正しくありません。

答えて

0

私は最善の方法は、代わりに適切なクラスを作成しているとxml

を永続化するために xstreamを使用してDOMツリーを作成し、

にかなり印刷オプションturendでそれを永続化すると思い

関連する問題