加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

XMLUtil

发布时间:2020-12-15 08:22:56 所属栏目:Java 来源:网络整理
导读:package TestLinkPackage; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintStream; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.Doc

package TestLinkPackage;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMLUtil{ private File workspaceFile; private File dirFile; private File buildXMLDirectory; private File htmlFileDirectory; private String workspacePath; private String buildXMLDirectoryPath; private String buildXMLFilePath; private String htmlFileDirectoryPath; private String tapPath; private List<Node> testResult = new ArrayList(); private List<Integer> sampleNumber = new ArrayList(); public void BuildXMLChange() { this.workspaceFile = new File(""); this.workspacePath = this.workspaceFile.getAbsolutePath(); this.dirFile = new File(this.workspacePath); File[] files = this.dirFile.listFiles(); this.buildXMLDirectoryPath = (this.workspacePath + "BuildXML"); this.buildXMLDirectory = new File(this.buildXMLDirectoryPath); File[] xmlFiles = this.buildXMLDirectory.listFiles(); this.buildXMLFilePath = (this.buildXMLDirectoryPath + "build.xml"); if (files.length != 0) { File[] arrayOfFile1; int j = (arrayOfFile1 = files).length; for (int i = 0; i < j; i++) { File file = arrayOfFile1[i]; if (file.getName().equals("BuildXML")) { if (xmlFiles.length != 0) { File[] arrayOfFile2; int m = (arrayOfFile2 = xmlFiles).length; for (int k = 0; k < m; k++) { File xmlFile = arrayOfFile2[k]; if (xmlFile.getName().equals("build.xml")) { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(this.buildXMLFilePath); NodeList nodeList = document.getElementsByTagName("property"); Node property = nodeList.item(0); NamedNodeMap nodeMap = property.getAttributes(); Node node = nodeMap.getNamedItem("value"); node.setTextContent(this.workspacePath); TransformerFactory factory = TransformerFactory.newInstance(); Transformer former = factory.newTransformer(); former.setOutputProperty("encoding","GBK"); try { former.transform(new DOMSource(document),new StreamResult(new File(this.buildXMLFilePath))); } catch (TransformerException e) { e.printStackTrace(); } System.out.println("build.xml文件不存在"); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (TransformerConfigurationException e1) { e1.printStackTrace(); } } } } else { System.out.println("BuildXML文件夹为空"); } } } } else { System.out.println("文件夹内容为空"); } } public void XMLToTAP() { this.workspaceFile = new File(""); this.workspacePath = this.workspaceFile.getAbsolutePath(); this.dirFile = new File(this.workspacePath); File[] files = this.dirFile.listFiles(); this.htmlFileDirectoryPath = (this.workspacePath + "XMLFolder"); this.htmlFileDirectory = new File(this.htmlFileDirectoryPath); File[] htmlFiles = this.htmlFileDirectory.listFiles(); this.tapPath = (this.workspacePath + "TAPFolder"); if (files.length != 0) { File[] arrayOfFile1; int j = (arrayOfFile1 = files).length; for (int i = 0; i < j; i++) { File file = arrayOfFile1[i]; if (file.getName().equals("XMLFolder")) { if (htmlFiles.length != 0) { File[] arrayOfFile2; int m = (arrayOfFile2 = htmlFiles).length; for (int k = 0; k < m; k++) { File xmlFile = arrayOfFile2[k]; if (xmlFile.getName().endsWith(".xml")) { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(xmlFile); Element rootElement = document.getDocumentElement(); NodeList nodeList = rootElement.getChildNodes(); if (rootElement.hasChildNodes()) { for (int i = 0; i < nodeList.getLength(); i++) { if (nodeList.item(i).getNodeType() == 1) { this.testResult.add(nodeList.item(i)); } } } else { System.out.println("接口测试结果为空"); } for (int i = 0; i < this.testResult.size(); i++) { if (((Node)this.testResult.get(i)).getNodeName().equals("sample")) { this.sampleNumber.add(Integer.valueOf(i)); } } for (int i = 0; i < this.sampleNumber.size(); i++) { NamedNodeMap namedNodeMap = ((Node)this.testResult.get(((Integer)this.sampleNumber.get(i)).intValue())).getAttributes(); if (namedNodeMap.getNamedItem("s").getNodeValue().equals("false")) { String lbValue = namedNodeMap.getNamedItem("lb").getNodeValue(); String[] fileContent = lbValue.split("-"); String fileNewPath = this.tapPath + "" + fileContent[0] + ".tap"; File fileNew = new File(fileNewPath); FileWriter fWriter = new FileWriter(fileNew,true); BufferedWriter bWriter = new BufferedWriter(fWriter); bWriter.write("TAP version 13"); bWriter.newLine(); bWriter.write("1..1"); bWriter.newLine(); String resultStr = "not ok 1"; bWriter.write(resultStr); bWriter.flush(); bWriter.close(); fWriter.close(); } else { String lbValue = namedNodeMap.getNamedItem("lb").getNodeValue(); String[] fileContent = lbValue.split("-"); String fileNewPath = this.tapPath + "" + fileContent[0] + ".tap"; File fileNew = new File(fileNewPath); FileWriter fWriter = new FileWriter(fileNew,true); BufferedWriter bWriter = new BufferedWriter(fWriter); bWriter.write("TAP version 13"); bWriter.newLine(); String resultStr = "1..1nok 1"; fWriter.write(resultStr); bWriter.flush(); bWriter.close(); fWriter.close(); } } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("xml文件不存在"); } } } else { System.out.println("XML文件夹为空"); } } } } else { System.out.println("文件夹内容为空"); } }}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读