Java解析xml文件和html文件

本文提供了一个Java示例程序,演示了如何解析XML文件并将其内容转换为Excel表格,以及如何使用Jsoup库从HTML页面中抽取特定的数据。
部署运行你感兴趣的模型镜像

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();
		}
	}


您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漁陽

彼此共勉,砥砺前行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值