<?xml version="1.0" encoding="UTF-8"?>
<generator>
<table name="login" operation="1">
<column name="username" handle="0">aaa</column>
<column name="password" handle="0">123</column>
</table>
<table name="login" operation="2">
<column name="id" handle="1">1</column>
<column name="username" handle="0">bbb</column>
<column name="password" handle="0">444</column>
</table>
<table name="login" operation="3">
<column name="id" handle="1">4</column>
</table>
</generator>
规则:<table>operation 1表insert,2表update,3表delete.
<column>handle 1表作为where条件,0表作为操作字段。
要求:按照规则生成三条SQL语句!(即做一个方法解析xml文件生成一个含有三条SQL语句的字符串)
import java.io.File;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class TestXML {
/**
* 解析XML文件生成一个含有可执行SQL语句的字符串
*
* @paramxmlFileName
* @returnSQL
*/
public static String parseXmltoSQL(String xmlFileName) {
StringBuffer sbsql = new StringBuffer();
SAXReader reader = new SAXReader();
try {
Document document = (Document) reader.read(new File(xmlFileName));
// Element element = document.getDocumentElement();
Element element = document.getRootElement();
Iterator it = element.elementIterator("table");
while (it.hasNext()) {
element = (Element) it.next();
// 获得对表的操作
String oper = element.attributeValue("operation");
// 获得表名
String tableName = element.attributeValue("name");
if ("1".equals(oper)) {
sbsql.append("insert into ").append(tableName);
Iterator it2 = element.elementIterator("column");
String columnName1 = null;
String columnValue1 = null;
String columnName2 = null;
String columnValue2 = null;
if (it2.hasNext()) {
element = (Element) it2.next();
columnName1 = element.attributeValue("name");
columnValue1 = element.getText();
}
if (it2.hasNext()) {
element = (Element) it2.next();
columnName2 = element.attributeValue("name");
columnValue2 = element.getText();
}
sbsql.append("(" + columnName1 + "," + columnName2 + ")"
+ " values('" + columnValue1 + "','" + columnValue2
+ "')\n");
} else if ("2".equals(oper)) {
sbsql.append("update ").append(tableName);
Iterator it2 = element.elementIterator("column");
String columnName1 = null;
String columnValue1 = null;
String columnName2 = null;
String columnValue2 = null;
String columnName3 = null;
String columnValue3 = null;
if (it2.hasNext()) {
element = (Element) it2.next();
columnName1 = element.attributeValue("name");
columnValue1 = element.getText();
}
if (it2.hasNext()) {
element = (Element) it2.next();
columnName2 = element.attributeValue("name");
columnValue2 = element.getText();
}
if (it2.hasNext()) {
element = (Element) it2.next();
columnName3 = element.attributeValue("name");
columnValue3 = element.getText();
}
sbsql.append(" set " + columnName2 + "='" + columnValue2
+ "'," + columnName3 + "='" + columnValue3
+ "' where " + columnName1 + "=" + columnValue1
+ "\n");
} else if ("3".equals(oper)) {
sbsql.append("delete from ").append(tableName);
Iterator it2 = element.elementIterator("column");
String columnName1 = null;
String columnValue1 = null;
if (it2.hasNext()) {
element = (Element) it2.next();
columnName1 = element.attributeValue("name");
columnValue1 = element.getText();
}
sbsql.append(" where " + columnName1 + "=" + columnValue1);
}
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sbsql.toString();
}
public static void main(String[] args) {
String str = "D://aa.xml";
String result = null;
try{
result = parseXmltoSQL(str);
System.out.println(result);
}catch(Exception e){
}
}
}
<doc>
<person id="1" sex="m">
<name>zhangsan</name>
<age>32</age>
<adds>
<add code="home">home add</add>
<add code="com">com add</add>
</adds>
</person>
<person id="2" sex="w">
<name>lisi</name>
<age>22</age>
<adds>
<add ID="22" id="23" code="home">home add</add>
<add ID="23" id="22" code="com">com add</add>
<add id="24" code="com">com add</add>
</adds>
</person>
</doc>
Node node1 = document.selectSingleNode("/doc/person");
System.out.println("输出节点:" + "\t" + node1.asXML());
Node node2 = document.selectSingleNode("/doc/person/@sex");
System.out.println("输出节点:" + "\t" + node2.asXML());
Node node3 = document
.selectSingleNode("/doc/person[name=\"zhangsan\"]/age");
System.out.println("输出节点:" + "\t" + node3.asXML());
System.out.println("\n---------XPath获取List节点测试------------");
List list = document
.selectNodes("/doc/person[name=\"zhangsan\"]/adds/add");
for (Iterator it = list.iterator(); it.hasNext();) {
Node nodex = (Node) it.next();
System.out.println(nodex.asXML());
}
System.out.println("\n---------通过ID获取元素的测试----------");
System.out.println("陷阱:通过ID获取,元素ID属性名必须为“大写ID”,小写的“id”会认为是普通属性!");
String id22 = document.elementByID("22").asXML();
String id23 = document.elementByID("23").asXML();
String id24 = null;
if (document.elementByID("24") != null) {
id24 = document.elementByID("24").asXML();
} else {
id24 = "null";
}
输出的都是xml格式的,要取得数据,还不清楚怎么操作