1.解析XML文件
public void getXml() {
try {
String nameT="";
File f = new File("C:\\Users\\dj\\Desktop\\test.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList nl = doc.getElementsByTagName("entry");
nameT = doc.getElementsByTagName("title").item(0).getFirstChild().getNodeValue();
System.out.println(doc.getElementsByTagName("title").item(0).getFirstChild().getNodeValue());
Workbook wb = new HSSFWorkbook();
Sheet sheet11 = wb.createSheet(nameT);// 第一个sheet
// 创建一行,在页sheet2上
Row row = sheet11.createRow((short) 0);
// 表头信息
Cell cell0 = row.createCell(0);
cell0.setCellValue("Job名称");
Cell cell1 = row.createCell(1);
cell1.setCellValue("最近执行时间");
Cell cell2 = row.createCell(2);
cell2.setCellValue("Case数");
// Cell cell3 = row.createCell(3);
// cell3.setCellValue("业务线");
int rowrwonum = 1;
for (int i = 0; i <= nl.getLength(); i++) {
if (i == 0) {
continue;
}
if (isContains(doc.getElementsByTagName("title").item(i).getFirstChild().getNodeValue())) {
Row rowrow = sheet11.createRow(rowrwonum++);
Cell row_c_1 = rowrow.createCell(0);
row_c_1.setCellValue(doc.getElementsByTagName("title").item(i).getFirstChild().getNodeValue());
Cell row_c_2 = rowrow.createCell(1);
row_c_2.setCellValue(DateString2formatString(
doc.getElementsByTagName("published").item(i - 1).getFirstChild().getNodeValue()));
}
// Cell row_c_3 = rowrow.createCell(3);
// row_c_3.setCellValue(nameT);
}
FileOutputStream fileOut = new FileOutputStream("C:\\Users\\dj\\Desktop\\LastBuild.xls");
// 把上面创建的工作簿输出到文件中
System.out.println("结束了");
wb.write(fileOut);
// 关闭输出流
fileOut.close();
wb.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
2.解析html
public void testTest() {
try {
AutotestResultEntity autotestResultEntity=resultService.getRecordByParameter("test", "941").getData();
org.jsoup.nodes.Document document = Jsoup.connect("http://jenkins.dj-inc.com/job/test/941/HTML_Report/overview.html").get();
// String title = document.title();
String text = document.text();
// System.out.println(title);
String s1 = text.substring(text.indexOf("总计 ") + 2, text.indexOf("报㝉"));
String s2 = s1.replaceAll(" ", "##");
// System.out.println(s2);
String s3 = s2.substring(2, s2.lastIndexOf("##"));
String s_1_3 = s3.substring(0, s3.indexOf("##"));
int s_1 = Integer.valueOf(s_1_3);
String s4 = s3.substring(s3.indexOf("##") + 2, s3.lastIndexOf("##"));
String s_4_1 = s4.substring(0, s4.indexOf("##"));
int s_2 = Integer.valueOf(s_4_1);
String s5 = s4.substring(s4.indexOf("##") + 2, s4.length());
int s_3 = Integer.valueOf(s5);
// System.out.println(s_1 + "**" + s_2 + "" + s_3);
autotestDsfResultEntity.setPassedCase(s_1);
autotestDsfResultEntity.setSkippedCase(s_2);
autotestDsfResultEntity.setFailedCase(s_3);
resultService.updateRecordByParameter(autotestDsfResultEntity);
// System.out.println(s1);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
本文提供了一个Java示例程序,演示了如何解析XML文件并将其内容转换为Excel表格,以及如何使用Jsoup库从HTML页面中抽取特定的数据。
509

被折叠的 条评论
为什么被折叠?



