0

I have many XML's in one directory with the same structure and I need to generate HTML table, where "result" from every XML will be one column. Before I had one csv file, so that was easy. But now I don't even know how to start. I read something about XSLT, but I don't know how to use this because I have the only path to directory with XML files, not the path to one file.

Example file1.xml

<test>
<filename>aa</filename>
<status>ok</status>
</test>

Example file2.xml

<test>
<filename>aa</filename>
<status>failed</status>
</test>

I need a table like this:

filename | status1 | status2  | status n
aaa      |   ok    |  not ok  |    n

Edit: Some old piece of code by guy from my team (excuse me for any ugly line of this legacy code, It was written a lot of time ago)

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++;
            }   
        }   
    }
}
arhu
  • 62
  • 3
  • 11
  • You can look at XPath to query xml nodes (very simple API) and to build the html, StringBuilder may be enough. – davidxxx Aug 08 '17 at 11:42
  • You can use Java to list the files in the directory and [apply your xslt to each](https://stackoverflow.com/questions/4604497/xslt-processing-with-java) while outputting into the same output file. – daniu Aug 08 '17 at 11:46
  • 1
    In XSLT 2.0 you can use the `collection()` function to process all files in a given directory at once, using a singles stylesheet. – michael.hor257k Aug 08 '17 at 13:25

1 Answers1

0

By seeing content of XML files, I will suggest creating a single XML file with child elements of "test".

Now for the reading and creating part, follow these links:

  1. Reading a XML file: What is the best/simplest way to read in an XML file in Java application?
  2. Writing HTML file: Write HTML file using Java

Since SO is an encouragement community, not code writing, you have to follow these links and create a useful code from it by yourself.

And yes we all are here to help you in your code. :-)

Aman
  • 735
  • 1
  • 6
  • 19
  • Thank you so much, I'll read these articles. I also found some old code in repository, which is working, but could you explain me how it works (I'll edit original post)? Author of this code is not available, so I can't even ask him and I'm not a java developer, just a DevOps. – arhu Aug 08 '17 at 11:58
  • 1
    Sure. Post that code and we will check what we can extract from it. – Aman Aug 08 '17 at 12:00