Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 以显示的xml格式显示输出_Java - Fatal编程技术网

Java 以显示的xml格式显示输出

Java 以显示的xml格式显示输出,java,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

我需要一个帮助来编写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)中有两个文件夹(文件夹_1和文件夹_2)。在文件夹_1中,我有两个文件“File_1”和“File_2”

在“文件1”中,我有7行代码,分别是第1行、第2行。。。第7行

我发现标签在很多地方都在关闭。我需要它在完成文件夹_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();
    }
} 
}
import java.io.*;
导入java.util.*;
导入java.lang.String;
公共类Vardeps{
公共静态void main(字符串args[])引发FileNotFoundException
{
//XML的文件编写器
文件写入程序流;
PrintWriter out=null;
字符串strOutputPath=“D:\\Current\\”;
字符串strFilePrefix=“Temp”;
字符串文件列表修剪;
int intID=1;
//读取系统的文件名
File startingDirectory=新文件(“D:\\Current\\\Additional\\文件\\abcd”);
List filesList=FileListing.getFileListing(startingDirectory);
文件文件=新文件(“D:\\Current\\\Additional\\\U Files\\abcd”);
File[]files=File.listFiles();
String[]children=file.list();
//试着用苏打系统。
试一试{
if(children==null)
{
//没有文件存在。
System.out.println(“不存在文件”);
}
其他的
{
//对于SodaSystem
fostream=newfilewriter(strOutputPath+“\\”+strFilePrefix+“.xml”);
out=新的PrintWriter(新的BufferedWriter(fostream));
请写出(“”);
请写出(“”);
//打印出系统的文件名
用于(文件名:filesList)
{
if(fileName.isDirectory()&&fileName.getName().endsWith(“vardeps”))
{
out.write(“\t\n”);
}
其他的
{
out.println(“\t\n”);
//这将保存关系CommonKey的文件名
fileListTrim=fileName.getName().substring(0,(fileName.getName().length()-4));
//打开第一个文件
//命令行参数
FileInputStream fstream=新的FileInputStream(文件名);
//获取DataInputStream的对象
DataInputStream in=新的DataInputStream(fstream);
BufferedReader br=新的BufferedReader(新的InputStreamReader(in));
弦斯特林;
//逐行读取文件
而((strLine=br.readLine())!=null)
{
//打印SodaVariable实体。
out.println(“\t”);
//打印SodaVariableDependency关系表。
out.println(“\t”);
intID++;
}
out.write(“\n\t\n”);
//关闭输入流
in.close();
}
//out.println(“/>”);
intID++;
out.write(“\n\t\n”);
}
}
out.println(“\n”);
out.flush();
out.close();
}
捕获(例外E){
E.printStackTrace();
}
} 
}

面临的问题:我正在体验的格式不正确。

我认为最好的方法是创建一个dom树,并使用漂亮的打印选项将其持久化

或者,创建一个适当的类并使用它来持久化xml