我在一个具有相同结构的目录中有很多XML,我需要生成HTML表,其中每个XML的“结果”都是一列。在我有一个csv文件之前,这很容易。但现在我甚至不知道如何开始。我读了一些关于XSLT的内容,但是我不知道如何使用它,因为我只有路径到XML文件的目录,而不是一个文件的路径。将多个xml文件合并为一个html表
例file1.xml
<test>
<filename>aa</filename>
<status>ok</status>
</test>
例file2.xml
<test>
<filename>aa</filename>
<status>failed</status>
</test>
我需要一个像这样的表:
filename | status1 | status2 | status n
aaa | ok | not ok | n
编辑:一些老片的代码由我的团队的人(原谅我的任何丑陋线的这种遗留代码,这是以前写了很多的时间)
public class Main {
private static ArrayList<TestSection> errors = new ArrayList<TestSection>();
private static int errorNum = 1;
public static void main(String[] args) throws JAXBException, IOException {
ArrayList<Test> tests = new ArrayList<Test>();
generateTestObjects(args, tests);
createHTML(tests, args[1]);
}
private static void generateTestObjects(String[] args, ArrayList<Test> arrayList) throws IOException{
Stream<Path> list = Files.list(Paths.get(args[0]));
list.forEach(arg0 -> {
try {
arrayList.add(unmarshall(arg0.toString()));
} catch (JAXBException e) {
e.printStackTrace();
}
});
list.close();
}
private static Test unmarshall(String fileName) throws JAXBException{
JAXBContext context = JAXBContext.newInstance(Test.class);
Unmarshaller un = context.createUnmarshaller();
Test test = (Test) un.unmarshal(new File(fileName));
return test;
}
private static void createHTML(ArrayList<Test> arraylist, String filename) throws IOException {
File file = new File("xunit.html");
FileWriter writer = new FileWriter(file);
writer.write("<!DOCTYPE html>");
writer.write("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
writer.write("<head>");
writer.write("<title>Xunit Report</title>");
writer.write("<meta http-equiv=\"content-Type\" content=\"text/html; charset=UTF-8\" />");
writer.write("</head>");
writer.write("<body>");
writer.write("<h1>Xunit Report</h1>");
createHTMLTable(arraylist, writer);
createHTMLTableCSV(filename, writer);
writer.write("</body>");
writer.write("</html>");
writer.close();
}
private static void createHTMLTable(ArrayList<Test> arraylist, FileWriter fwriter) throws IOException{
fwriter.write("<table border=\"1\">");
fwriter.write("<tr bgcolor=\"#A9A9F5\">");
fwriter.write("<th style=\"text-align:left\">Test case: </th>");
List<TestSection> testSections = arraylist.stream().map(Test::getTestSection).collect(Collectors.toList());
for(int file=0; file < arraylist.size(); file++){
fwriter.write("<th style=\"text-align:left\">" + arraylist.get(file).getAppInformation().getWorkstationId() + "</th>");
}
for(int test=0; test < testSections.get(0).getTests().size(); test++){
fwriter.write("<tr>");
String testName = testSections.get(0).getTests().get(test).getTestName();
testName = testName.replace("<", "<");
testName = testName.replace(">", ">");
fwriter.write("<td>" + testName + "</td>");
for(int file=0; file < arraylist.size(); file++){
String status = testSections.get(file).getTests().get(test).getStatus();
if(status.equalsIgnoreCase("PASSED")){
fwriter.write("<td bgcolor=\"#00FF00\">" + status + "</td>");
} else{
fwriter.write("<td bgcolor=\"#FF4000\">" +"<a href=\"#"+errorNum+"error\">"+ status + "</a></td>");
errorNum++;
}
}
fwriter.write("</tr>");
}
fwriter.write("</table> <br/> <br/>");
for(Test test: arraylist){
int id = 1;
if(test.getTestSection().getFailedTests() != null){
for(int i=0;i<test.getTestSection().getFailedTests().size();i++){
String testName = test.getTestSection().getFailedTests().get(i).getTestName();
testName = testName.replace("<", "<");
testName = testName.replace(">", ">");
fwriter.write("<h4 id=\""+id+"error\">" + testName +" "+test.getAppInformation().getWorkstationId()+"</h4>");
fwriter.write("<h5>"+ test.getTestSection().getFailedTests().get(i).getDetails()+"</h5>");
id++;
}
}
}
}
你可以看看XPath来查询xml节点(非常简单的API)并且构建html,StringBuilder就足够了。 – davidxxx
您可以使用Java来列出目录中的文件并[将您的xslt应用于每个文件](https://stackoverflow.com/questions/4604497/xslt-processing-with-java),同时输出到相同的输出文件中。 – daniu
在XSLT 2.0中,您可以使用'collection()'函数一次处理给定目录中的所有文件,并使用单个样式表。 –