2011-07-06 65 views
0

顯示輸出,我需要幫助以書面的Java代碼顯示在以下格式的輸出,中顯示的XML格式

請幫我糾正這個Java文件和打印以正確的格式輸出。

<?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

我認爲最好的方法是創建一個DOM樹,漂亮的打印選項turend堅持它

或者已經創建了一個合適的類,並使用xstream堅持XML