1概述
在進行ESB集成項目中,使用到了不少系統的接口,這些接口傳輸的數據大部分都採用了XML的格式,這樣在使用ESB開發服務時就須要對XML數據進行解析或拼接的操做,本文以項目中流程服務爲例,講解一些經常使用的Dom4j對XML的操做。
2名詞解釋
Dom4j:一個Java的XML API,用來讀寫XML文件的,具備性能優異、功能強大和極端易用使用的特色。
使用Dom4j須要使用對應的jar包,官網下載地址:
http://www.dom4j.org/dom4j-1.6.1/
Dom4jAPI地址:
http://www.oschina.net/uploads/doc/dom4j-1.6.1/index.html
AEAI ESB:應用集成平臺主要做爲企業信息系統的「龍骨」來集成各業務系統,通常稱之爲企業服務總線(Enterprise Service BUS,ESB),在數通暢聯軟件的產品家族中應用集成平臺命名爲AEAI ESBhtml
3操做方法
在ESB流程中須要先查詢出第三方系統須要的數據,再進行XML格式化處理後,調用第三方系統提供的Web Service服務,這裏須要建立一個XML格式的數據。
3.1建立Document對象
這裏採用如下方法主動建立document對象app
Document document = DocumentHelper.createDocument();dom |
查詢相關API發現還有另外兩種建立對象的方法
1.讀取XML文件,得到document對象工具
SAXReader reader = new SAXReader();性能 Document document = reader.read(new File("csdn.xml"));this |
2.解析XML形式的文本,獲得document對象編碼
String text = "XXXX";spa Document document = DocumentHelper.parseText(text);.net |
3.2節點操做
建立document後,添加第一個節點orm
Element dataElement = document.addElement("DATA"); |
這個節點做爲根節點
這裏經過API來講明一下其餘節點操做的方法
1.獲取根節點
Element root = document.getRootElement();
2.取得某個節點的子節點.
Element element= root.element(「REQUESTDATA」);
3.取得節點的內容
String text= element.getText();
4.取得某節點下全部名爲」 REQUESTDATA」的子節點,並進行遍歷
List elements = rootElm.elements("csdn"); for (Iterator it = elements.iterator(); it.hasNext();) { Element elm = (Element) it.next(); //操做處理 } |
5.對某節點下的全部子節點進行遍歷
for(Iterator it=root.elementIterator();it.hasNext();){ Element element = (Element) it.next(); //操做處理 } |
6.設置節點文字
element.setText("XXXX"); |
7.刪除某節點
childElement是待刪除的節點 parentElement是其父節點 parentElement.remove(childElment); |
8.添加一個CDATA節點
在拼接完整的soap請求體時,涉及到soap請求體中添加XML格式數據時,不須要soap協議進行解析的內容,須要添加CDATA節點
Element contentElm = infoElm.addElement("content"); |
3.3屬性操做
添加了節點後,須要在節點中進行說明此節點的含義,這時須要給這個節點添加屬性
經過相關API找到其餘的屬性操做方法
1.取得某節點下的某屬性
element.addAttribute("屬性名 ", "內容"); |
2.取得屬性的內容
String text=attribute.getText(); |
3.刪除某屬性
Attribute attribute=root.attribute("屬性名"); root.remove(attribute); |
4.設置屬性的內容
Attribute attribute=root.attribute("屬性名"); attribute.setText("內容"); |
5.遍歷某節點的全部屬性
Element root=document.getRootElement(); for(Iterator it=root.attributeIterator();it.hasNext();){ Attribute attribute = (Attribute) it.next(); //操做處理 } |
3.4XML和字符串轉換
拼好XML格式數據後,須要調用接口,而接口的入參類型爲String,這裏須要將拼好的Document對象轉換爲字符串,方式爲
String docText=document.asXML(); |
而將XML格式的字符串轉換爲document對象的方式爲
String text = "XML格式數據"; Document document = DocumentHelper.parseText(text); |
4其餘說明
4.1文檔操做
1.全爲英文
XMLWriter writer = new XMLWriter(new FileWriter("allEnglish.xml")); writer.write(document); writer.close(); |
2.含有中文
OutputFormat format = OutputFormat.createPrettyPrint(); // 建立文件輸出的時候,自動縮進的格式 format.setEncoding("UTF-8"); //設置編碼 XMLWriter writer = new XMLWriter(newFileWriter("contentChinese.xml"),format); writer.write(document); writer.close(); |
5實例代碼
流程服務涉及到的部分相關代碼
建立document對象並拼接XML
DataRow headerDataRow = (DataRow) this.getVariable("headerDataRow").getValue(); // XmlUtil是一個XML操做工具類,在數通暢聯產品內置的jar包中 Document document = XmlUtil.createDocument(); Element dataElement = document.addElement("DATA"); dataElement.addElement("REQUESTDATA"); Element datainfosElement = dataElement.addElement("DATAINFOS"); datainfosElement.addAttribute("REMARK", "主表"); Element danhaoElement = datainfosElement.addElement("DANHAO"); danhaoElement.addAttribute("REMARK", "單號"); danhaoElement.addText(headerDataRow.getString("APPLY_NUMBER")); Element xingmingElement = datainfosElement.addElement("XINGMING"); xingmingElement.addAttribute("REMARK", "姓名"); xingmingElement.addText(headerDataRow.getString("APPLY_USER_NAME")); |
解析接口返回的XML格式數據,並判斷ESB_CODE節點的內容
//getVariable是ESB中封裝好的方法,用於經過CODE值獲取流程變量 String soapResponse = (String) this.getVariable("soapResponse").getValue(); Document document = DocumentHelper.parseText(soapResponse); Element root = document.getRootElement(); Element resultCode = root.element("ESB_CODE"); int resultMark = 0; if("S".equals(resultCode.getText())){ resultMark = 1; int applyId = (Integer) this.getVariable("applyId").getValue();
DataRow updateRow = new DataRow("APPLY_ID",applyId,"OA_FLAG","N"); this.getVariable("updateRow").setValue(updateRow); } |
注:附件爲操做樣例工程代碼,解析Project.xml文件並對其進行增刪改查操做。產品及附件http://pan.baidu.com/s/1kVCt5L9