java - 修改嵌套for循环,根据xml标签创建多个xml文件
问题描述
我有一些从 excel 文件 (CSV) 中获取的数据。对于 excel 文件中的每一行,我想生成一个不同的 xml 文件。
我编写的函数从 CSV 文件和文件名中获取一串标题和一个数组列表。我认为在我的 for 循环中循环遍历 CSV 文件中的字符串。我需要创建一个语句,该语句可以根据 excel 文件中的行数或基于 xml 标签创建多个 xml 文件,然后我需要在我的 main 方法中修改该链。我需要一些关于如何创建该语句的指导。下面附上我的 in.csv 文件。每个分割后的文件应命名为descriptor_pdx01.xml、descriptor_pdx02.xml、descriptor_pdx03.xml ......等。
这里也是 in.csv 文件的内容。
TESTCLIENT_2017_09_30 pdx01 Carcinosarcoma C34448 M 12
TESTCLIENT_2017_09_30 pdx02 Esophageal carcinoma C4025
TESTCLIENT_2017_09_30 pdx03 Esophageal carcinoma C4025 45
TESTCLIENT_2017_09_30 pdx04 Carcinosarcoma C34448 F
TESTCLIENT_2017_09_30 pdx05 Esophageal carcinoma C4025 F 60
TESTCLIENT_2017_09_30 pdx06 Esophageal carcinoma C4025 F 66
TESTCLIENT_2017_09_30 pdx07 Carcinosarcoma C34448 M 70
我的main方法和wrtietoXml方法贴在下面有什么想法告诉我
import java.io.BufferedReader;
import java.util.ArrayList;
public class CsvToXml {
public static void main (String Args[]){
//This is hard coded, in a future version I would consider stripping this from a command-line arg.
BufferedReader bufferedCsvFile = HelperMethods.getCsvFileBuffer("/Users/edgarjohnson/eclipse-workspace/CsvToXml/in.csv");
ArrayList<String> csvFileStrings = new ArrayList<String>();
HelperMethods.readCsvToStrings(csvFileStrings, bufferedCsvFile);
String[] columnHeaders = HelperMethods.setHeaders(csvFileStrings);
csvFileStrings.remove(0); // Remove headers from the csvStrings Arraylist
HelperMethods.writeXmlFile(columnHeaders, csvFileStrings, "xmlOutput.xml");
}
}
public static void writeXmlFile(String[] headers, ArrayList <String> stringsFromCsv, String fileName){
try {
BufferedWriter buffWrite = new BufferedWriter(new FileWriter(fileName));
buffWrite.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
buffWrite.write("<patient>\r\n");
//String array that is the same size is that of the string from csv from line 66-69
//For each string in the csv file
for(String s:stringsFromCsv){
buffWrite.write("\t<person>\r\n");
//Split the line into an array of strings
String fields[] = s.split(",");
//For each item in that array of strings
for(int i=0; i<fields.length; i++){
//Define a String and keep on adding to that string using field element array, String should be outside for loopLol
//Write the corresponding header to the file, as well as the value from the array 'fields'
buffWrite.write("\t\t<" + headers[i] +">"+ fields[i] + "</" + headers[i] +">\n");
}
buffWrite.write("\t</person>\n");
}
buffWrite.write("</people>");
buffWrite.close();
}
catch (IOException ioe){ System.err.println("Error while writing to xml file in writeXmlFile: "); ioe.printStackTrace();
}
}
解决方案
推荐阅读
- javascript - 在一组按钮中激活单个按钮
- batch-file - 批处理/查找和编辑 TXT 或 XML 文件中的特定位置并将其替换为随机
- loops - 从数据库查询中将数据移动到维度结构中
- linux - 如何通过 shell 脚本在 Linux 操作系统中创建多用户?
- python - 在python中汇总时过滤
- python - Python - Big(O) 运行时:列表理解和 For 循环
- javascript - 即使加载 1000 条记录,光滑的网格也太慢了
- c# - 从客户端获取 C# 中的当前 IP 地址
- c# - 使用 C# 将数据添加到我的 Sql 数据时出现“程序需要未提供的参数”错误
- jmeter - Jmeter工具,线程循环计数是永远的,线程数1以增量方式跳过循环控制器中的html页面