一、需求:
将数据库中存储的二进制数组内容读出,转换成Document,并解析,将其中某个标签的值修改后再转换成二进制数组保存到数据库中。
数据转换过程如下:
byte[ ] -> String(此处是xml字符串) -> Document -> 解析文档并修改;
Document -> String(此处是xml字符串) ->byte[ ] ;
二、实现过程:
1.要解析的xml数据格式。
对标签中标签的内容做修改,修改后再转换成byte[ ] 保存到 mysql。
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.3">
<hashTree>
<hashTree>
<hashTree>
<OnceOnlyController guiclass="OnceOnlyControllerGui" testclass="OnceOnlyController" testname="仅一次控制器" enabled="true"/>
<hashTree>
<CSVDataSet guiclass="TestBeanGUI" testclass="CSVDataSet" testname="CSV Data Set Config" enabled="true">
<stringProp name="filename">/Users/xiaojuanzhang/byxf/workFile/测试平台/王琪接口测试用例/1.csv</stringProp>
<stringProp name="fileEncoding"></stringProp>
<stringProp name="variableNames"></stringProp>
<boolProp name="ignoreFirstLine">false</boolProp>
<stringProp name="delimiter">,</stringProp>
<boolProp name="quotedData">false</boolProp>
<boolProp name="recycle">true</boolProp>
<boolProp name="stopThread">false</boolProp>
<stringProp name="shareMode">shareMode.all</stringProp>
</CSVDataSet>
<hashTree/>
<CSVDataSet guiclass="TestBeanGUI" testclass="CSVDataSet" testname="CSV Data Set Config" enabled="true">
<stringProp name="delimiter">,</stringProp>
<stringProp name="fileEncoding">utf-8</stringProp>
<stringProp name="filename">C:\Users\wangqi\Desktop\1.csv</stringProp>
<boolProp name="quotedData">false</boolProp>
<boolProp name="recycle">true</boolProp>
<stringProp name="shareMode">shareMode.all</stringProp>
<boolProp name="stopThread">false</boolProp>
<stringProp name="variableNames">userId,scopeId,source,engine,operate,db,table</stringProp>
<boolProp name="ignoreFirstLine">false</boolProp>
</CSVDataSet>
<hashTree/>
</hashTree>
</hashTree>
</hashTree>
</hashTree>
</jmeterTestPlan>
2.代码
//import org.w3c.dom.*;
private void updateAttachmentPath(String operator,Integer jmeterCaseId,String path) throws ParserConfigurationException, IOException, SAXException, TransformerException {
//1.从数据库查寻需要处理的脚本二进制数据;
TmpJmeterCasePo jmeterCasePo = this.getById(jmeterCaseId);
byte[] casePoFileData = jmeterCasePo.getFileData();
//2.二进制字节数组转换成 xml 字付串;
String jmxStr = new String(casePoFileData);
//3.将 xml字符串解析成Document(此处用的 org.w3c.dom.*;)
//3.1)获取Document的构造器工厂实例
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
//3.2)创建XML构造器的DocumentBuilder实例
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
// 3.3)Document文档对象
Document doc = docBuilder.parse(new InputSource(new StringReader(jmxStr)));
//4.解析文档;
//4.1定位需要处理的标签;
NodeList csvDataSet = doc.getElementsByTagName("CSVDataSet");
for(int i = 0;i<csvDataSet.getLength();i++){
Element e = (Element)csvDataSet.item(i);
NodeList stringProps = e.getElementsByTagName("stringProp");
for(int m =0;m<stringProps.getLength();m++){
Node node = stringProps.item(m);
String filename = node.getAttributes().item(0).getNodeValue();
if(StringUtils.isNotBlank(filename) && "filename".equals(filename)){
//4.2 修改标签内容;
node.setTextContent(path);
}
}
}
//5.将 Document文档转成 xml字符串;
StringWriter writer = new StringWriter();
javax.xml.transform.TransformerFactory.newInstance().newTransformer().transform(new javax.xml.transform.dom.DOMSource(doc), new javax.xml.transform.stream.StreamResult(writer));
String str = writer.toString();
jmeterCasePo.setFileData(str.getBytes());
this.updateJmeterCase(jmeterCasePo,operator);
}