将多个xml文件放入一个html表中

时间:2017-08-08 11:40:00

标签: java xml xslt

我在一个具有相同结构的目录中有很多XML,我需要生成HTML表格,其中"结果"从每个XML都将是一列。在我有一个csv文件之前,这很容易。但现在我甚至不知道如何开始。我读了一些关于XSLT的内容,但我不知道如何使用它,因为我只有到XML文件目录的路径,而不是一个文件的路径。

示例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("<", "&lt");
        testName = testName.replace(">", "&gt");
        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("<", "&lt");
                testName = testName.replace(">", "&gt");
                fwriter.write("<h4 id=\""+id+"error\">" + testName +"       "+test.getAppInformation().getWorkstationId()+"</h4>");
                fwriter.write("<h5>"+ test.getTestSection().getFailedTests().get(i).getDetails()+"</h5>");
                id++;
            }   
        }   
    }
}

1 个答案:

答案 0 :(得分:0)

通过查看XML文件的内容,我建议创建一个子元素为“test”的XML文件。

现在阅读和创建部分,请点击以下链接:

  1. 阅读XML文件:What is the best/simplest way to read in an XML file in Java application?
  2. 撰写HTML文件:Write HTML file using Java
  3. 由于SO是鼓励社区,而不是代码编写,因此您必须遵循这些链接并自行创建有用的代码。

    是的,我们都在这里为您的代码提供帮助。 : - )