解析xml文件,並將xml文件中的資料拼接成insert語句
education.xml的內容是:
<?xml version="1.0" encoding="utf-8"?>
<Msg>
<Head>
<Id>20140529100039</Id>
<Name>廈煙人事子集查詢回覆</Name>
<Source>HR</Source>
<Target>XYRC</Target>
<SerName>ws_inferConnect</SerName>
<MsgType>2</MsgType>
<RtCode>1</RtCode>
<RtDesc></RtDesc>
<Backup1></Backup1>
<Date>2018/09/20 15:40:21</Date>
<Attrs>
<Attr desc='子集名' illus='SPECIALTY_TEC'>SPECIALTY_TEC</Attr>
<Attr desc='人員ID' illus='0'>70025</Attr>
<Attr>abcd</Attr>
</Attrs>
</Head>
<DATA>
<TABLE TABLENAME='SPECIALTY_TEC' BASIC_ID='人員ID' NAME='姓名' ZYJSRJZG='專業技術資格名稱' QUACHANLL='取得資格途徑' QUADATEE='取得資格日期' TESTNAME='資格審批單位' POSORDER='專業技術職務名稱' ZYJSZWDJ='專業技術資格等級' REM='備註'>
<ROW ACTION='QUERY' BASIC_ID='70025' NAME='吳志文' ZYJSRJZG='助理工程師' QUACHANLL='確認' QUADATEE='1992-08-01 00:00' TESTNAME='廈門捲菸廠職稱改革領導小組' POSORDER='工程技術人員' ZYJSZWDJ='助理級(助教級)' REM=''/>
<ROW ACTION='QUERY' BASIC_ID='70025' NAME='吳志文' ZYJSRJZG='高階經濟師' QUACHANLL='評審' QUADATEE='2007-11-22 00:00' TESTNAME='中國菸草總公司高階經濟師任職資格評審委員會' POSORDER='經濟人員' ZYJSZWDJ='副高階(副教授級)' REM=''/>
<ROW ACTION='QUERY' BASIC_ID='70025' NAME='吳志文' ZYJSRJZG='工程師' QUACHANLL='評審' QUADATEE='1997-11-21 00:00' TESTNAME='福建省菸草專賣局職稱改革領導小組' POSORDER='工程技術人員' ZYJSZWDJ='中級(講師級)' REM=''/>
</TABLE>
</DATA>
</Msg>
開始解析
public class AnalysisXmlAction {
public static void main(String[] args) {
SAXReader saxReader = new SAXReader();
Document document;
String xmlString = "";
try {
document = saxReader.read(new File("src/main/resources/education.xml"));
xmlString = document.asXML();// 將xml內容轉化為字串
} catch (Exception e) {
e.printStackTrace();
xmlString = "";
}
new AnalysisXmlAction().analysisXml(xmlString);
}
@SuppressWarnings("rawtypes")
public void analysisXml(String str) {
List<String> sqlArray = new ArrayList<String>();
try {
// 通過reader物件的read方法載入books.xml檔案,獲取docuemnt物件。
Document document = DocumentHelper.parseText(str);
// 通過document物件獲取根節點bookstore
Element bookStore = document.getRootElement();
// 通過element物件的elementIterator方法獲取迭代器
Iterator oneLev = bookStore.elementIterator(); // 第一層下面的子集
// 遍歷迭代器,獲取根節點中的資訊
while (oneLev.hasNext()) {
Element data = (Element) oneLev.next();
// 獲取book的屬性名以及 屬性值
Iterator twoLev = data.elementIterator(); // 獲取子節點
String sql = "";
while (twoLev.hasNext()) {
Element bookChild = (Element) twoLev.next();
String name = bookChild.getName();
@SuppressWarnings("unchecked")
List<Attribute> twoLevAttr = bookChild.attributes();
if ("TABLE".equalsIgnoreCase(name)) {
for (Attribute attr : twoLevAttr) {// 遍歷當前節點的所有屬性
String attrName = attr.getName();// 屬性名稱
String attrValue = attr.getValue();
if (!"TABLENAME".equalsIgnoreCase(attrName)) {
sql += attrName + "," ;
} else {
sql += "insert into RL_" + attrValue + "(id_key,";
}
}
sql = sql.substring(0, sql.length() - 1);
sql += ") values (xyrc.seq_cnp_idkey.nextval,";
Iterator threeLev = bookChild.elementIterator();
while (threeLev.hasNext()) {
String insertSql = sql;
Element bookChildAttrs = (Element) threeLev.next();
@SuppressWarnings("unchecked")
List<Attribute> listAttr = bookChildAttrs.attributes();
for (Attribute attr : listAttr) {// 遍歷當前節點的所有屬性
String value = attr.getValue();// 屬性的值
if (!"QUERY".equalsIgnoreCase(value)) {
insertSql += "'" + value + "',";
}
}
insertSql = insertSql.substring(0, insertSql.length() - 1);
insertSql += ")";
sqlArray.add(insertSql);
System.out.println("insertSql:" + insertSql);
}
}
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
//analysisXmlService.insertAll(sqlArray);
}
}
最後拼成的sql語句是:
insert into RL_SPECIALTY_TEC(id_key,BASIC_ID,NAME,ZYJSRJZG,QUACHANLL,QUADATEE,TESTNAME,POSORDER,ZYJSZWDJ,REM) values (xyrc.seq_cnp_idkey.nextval,'70025','吳志文','助理工程師','確認','1992-08-01 00:00','廈門捲菸廠職稱改革領導小組','工程技術人員','助理級(助教級)','')