开发者

displaying output in the displayed xml format

I need a help in writing the java code to display the output in the following format,

Please help me in correcting this java file and print the output in the correct format.

<?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_开发者_Python百科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>

I have 2 folders(Folder_1 and Folder_2) inside a folder(abcd). Inside the Folder_1, I have 2 files "File_1" and "FIle_2".

Inside "file_1", i have 7 lines of code liek Line_1, Line_2... Line_7...

I am finding the tag closing in many places. I need that to be closed when it finishes the Folder_1 only.

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();
    }
} 
}

Issue faced : the format is not proper which i am expeccting.


I think best way is to create a dom tree and persist it with pretty printing option turend on

Alternatively have a proper class created and use xstream to persist xml

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜