2017-08-08 168 views
0

我在一个具有相同结构的目录中有很多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("<", "&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++; 
      } 
     } 
    } 
} 
+0

你可以看看XPath来查询xml节点(非常简单的API)并且构建html,StringBuilder就足够了。 – davidxxx

+0

您可以使用Java来列出目录中的文件并[将您的xslt应用于每个文件](https://stackoverflow.com/questions/4604497/xslt-processing-with-java),同时输出到相同的输出文件中。 – daniu

+1

在XSLT 2.0中,您可以使用'collection()'函数一次处理给定目录中的所有文件,并使用单个样式表。 –

回答

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

由于SO是一种鼓励社区,不代码编写,你必须遵循这些链接并自己创建一个有用的代码。

是的,我们都在这里帮助你的代码。 :-)

+0

感谢您的赞赏:-) – Aman

+0

非常感谢,我会阅读这些文章。我还在库中找到了一些旧的代码,它可以工作,但是你能否解释它是如何工作的(我将编辑原始文章)?此代码的作者不可用,所以我甚至不能问他,我不是一个Java开发人员,只是一个DevOps。 – arhu

+1

当然。发布该代码,我们将检查我们可以从中提取的内容。 – Aman

相关问题