以显示的xml格式显示输出

时间:2011-07-06 13:37:59

标签: java

我需要帮助编写java代码,以下列格式显示输出,

请帮我纠正这个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行代码谎言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();
    }
} 
}

问题面临:格式不正确,我正在考虑。

1 个答案:

答案 0 :(得分:0)

我认为最好的方法是创建一个dom树并使用漂亮的打印选项turend保持它

或者创建一个合适的类并使用xstream来持久化xml