1.Document 与 String之间的相互转换
我这里只是进行了String转换为Document。
//string 转换成inputstream
StringReader sr = new StringReader(ScriptContent);
InputSource is = new InputSource(sr);
Document document = (Document) db.parse(is);
具体还有其他几种方法,参考博客:
https://blog.csdn.net/weixin_29111593/article/details/114065434
2.XML解析
这个博客写的很清楚:
https://www.cnblogs.com/longqingyang/p/5577937.html
还有其他的一个博客,可以相比较学习一下:
https://blog.csdn.net/p812438109/article/details/81807440
多级XML解析方案,这个博客写的很详细:
https://www.cnblogs.com/qinyios/p/11377081.html
不过我没有采用这种方法,我选择了使用类来进行存取,感觉比较麻烦,后面看看要不要改进为上面的map来进行存取。
下面介绍一下我写的这一块儿。
使用的XML脚本如下:
<SQLTEST>
<CONTENT>单行函数ACOS的测试</CONTENT>
<ADMIN_USERID>USERNAME</ADMIN_USERID>
<ADMIN_PASSWD>PASSWORD</ADMIN_PASSWD>
<CONNECT>0</CONNECT>
<ENVIRONMENT>
<SETCONNECTID>0</SETCONNECTID>
<SQL_CASE>
<TYPE>DIRECT_EXECUTE_SUCCESS</TYPE>
<SQL>Create Table TEST_NUM(ID int primary KEY,testNum int);</SQL>
<TYPE>DIRECT_EXECUTE_SUCCESS</TYPE>
<SQL>INSERT INTO TEST_NUM VALUES (1,-15);</SQL>
</SQL_CASE>
</ENVIRONMENT>
</SQLTEST>
1)有直接获取到标签CONTENT的方法,不过不是很推荐
NodeList nodeList = document.getElementsByTagName("CONTENT");
strValue = nodeList.item(0).getFirstChild().getNodeValue();
2)获取到多层标签的内容
NodeList nodeList = document.getElementsByTagName("SQLTEST");
for(int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
NodeList childNodesSQLTEST = node.getChildNodes();
for(int j = 0; j < childNodesSQLTEST.getLength(); j++) {
if(childNodesSQLTEST.item(j).getNodeType() == Node.ELEMENT_NODE) {
Node nodeTmp = childNodesSQLTEST.item(j);
String strNodeName = nodeTmp.getNodeName();
String strNodeValue = nodeTmp.getFirstChild().getNodeValue();
System.out.println("标签:" +strNodeName + " 值:" + strNodeValue);
if(strNodeName.equals("ENVIRONMENT") || strNodeName.equals("IMPLEMENT") ||
strNodeName.equals("RESULT_COMPARE")) {
NodeList nodesTmp = nodeTmp.getChildNodes();
for(int k = 0; k < nodesTmp.getLength(); k++) {
if(nodesTmp.item(k).getNodeType() == Node.ELEMENT_NODE) {
nodeTmp = nodesTmp.item(k);
strNodeName = nodeTmp.getNodeName();
strNodeValue = nodeTmp.getFirstChild().getNodeValue();
System.out.println(" 标签:" +strNodeName + " 值:" + strNodeValue);
parseSecondLabel(nodeTmp);
}
}
}
private void parseSecondLabel(Node node) {
String strNodeName = node.getNodeName();
String strNodeValue = node.getFirstChild().getNodeValue();
//System.out.println("标签:" +strNodeName + " 值:" + strNodeValue);
if(strNodeName.equals("CONTENT")) {
scriptLabel.setCONTENT(strNodeValue);
}
else if(strNodeName.equals("ADMIN_USERID")) {
scriptLabel.setADMIN_USERID(strNodeValue);
}
else if(strNodeName.equals("ADMIN_PASSWD")) {
scriptLabel.setADMIN_USERID(strNodeValue);
}
else if(strNodeName.equals("CONNECT")) {
scriptLabel.setCONNECT(strNodeValue);
}
else if(strNodeName.equals("USER_ID")) {
scriptLabel.setUSER_ID(strNodeValue);
}
else if(strNodeName.equals("PASSWD")) {
scriptLabel.setPASSWD(strNodeValue);
}
else if(strNodeName.equals("SETCONNECTID")) {
scriptLabel.setSETCONNECTID(strNodeValue);
}
else if(strNodeName.equals("SQL_CASE")) {
NodeList nodeList = node.getChildNodes();
ScriptThirdLabel scriptThirdLabel = new ScriptThirdLabel();
for(int i = 0; i < nodeList.getLength(); i++) {
if(nodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
Node nodeTmp = nodeList.item(i);
strNodeName = nodeTmp.getNodeName();
strNodeValue = nodeTmp.getFirstChild().getNodeValue();
System.out.println(" 标签:" +strNodeName + " 值:" + strNodeValue);
parseThirdLabel(nodeTmp, scriptThirdLabel);
}
}
}
}
package menu;
import java.util.ArrayList;
import java.util.List;
public class ScriptSecondLabel {
private List<String> CONTENT;
private List<String> ADMIN_USERID;
private List<String> ADMIN_PASSWD;
private List<String> CONNECT;
private List<String> USER_ID;
private List<String> PASSWD;
private List<String> SETCONNECTID;
private List<ScriptThirdLabel> SQL_CASE;
ScriptSecondLabel(){
CONTENT = new ArrayList<String>();
ADMIN_USERID = new ArrayList<String>();
ADMIN_PASSWD = new ArrayList<String>();
USER_ID = new ArrayList<String>();
PASSWD = new ArrayList<String>();
CONNECT = new ArrayList<String>();
SETCONNECTID = new ArrayList<String>();
SQL_CASE = new ArrayList<ScriptThirdLabel>();
}
public void setCONTENT(String CONTENT) {
this.CONTENT.add(CONTENT);
}
public List<String> getCONTENT() {
return CONTENT;
}
public void setADMIN_USERID(String ADMIN_USERID) {
this.ADMIN_USERID.add(ADMIN_USERID);
}
public List<String> getADMIN_USERID() {
return ADMIN_USERID;
}
public void setADMIN_PASSWD(String ADMIN_PASSWD) {
this.ADMIN_PASSWD.add(ADMIN_PASSWD);
}
public List<String> getADMIN_PASSWD() {
return ADMIN_PASSWD;
}
public void setCONNECT(String CONNECT) {
this.CONNECT.add(CONNECT);
}
public List<String> getCONNECT() {
return CONNECT;
}
public void setUSER_ID(String USER_ID) {
this.USER_ID.add(USER_ID);
}
public List<String> getUSER_ID() {
return USER_ID;
}
public void setPASSWD(String PASSWD) {
this.PASSWD.add(PASSWD);
}
public List<String> getPASSWD() {
return PASSWD;
}
public void setSETCONNECTID(String SETCONNECTID) {
this.SETCONNECTID.add(SETCONNECTID);
}
public List<String> getSETCONNECTID() {
return SETCONNECTID;
}
public void setSQL_CASE(ScriptThirdLabel SQL_CASE) {
this.SQL_CASE.add(SQL_CASE);
}
public List<ScriptThirdLabel> getSQL_CASE() {
return SQL_CASE;
}
}