先來看看dom4j中相應XML的DOM樹建立的繼承關系
針對於XML標准定義。相應於圖2-1列出的內容,dom4j提供了下面實現:
以下給出一個詳細事例:
package com.iboxpay.settlement.gateway.common.util;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import com.iboxpay.settlement.gateway.ccb.Constant;
import com.iboxpay.settlement.gateway.common.exception.PackMessageException;
import com.iboxpay.settlement.gateway.common.exception.ParseMessageException;
/**
* DOM4j工具類
* @author caolipeng
*/
public class DomUtil {
/**
* 加入孩子節點元素
* @param parent 父節點
* @param childName 孩子節點名稱
* @param childValue 孩子節點值
* @return 新增節點
*/
public static Element addChild(Element parent, String childName,
String childValue) {
Element child = parent.addElement(childName);//加入節點元素
child.setText(childValue == null ?
"" : childValue); //為元素設值
return child;
}
/**
* DOM4j的Document對象轉為XML報文串
* @param document
* @param charset
* @return 經過解析后的xml字符串
*/
public static String documentToString(Document document,String charset) {
StringWriter stringWriter = new StringWriter();
OutputFormat format = OutputFormat.createPrettyPrint();//獲得格式化輸出流
format.setEncoding(charset);//設置字符集,默覺得UTF-8
XMLWriter xmlWriter = new XMLWriter(stringWriter, format);//寫文件流
try {
xmlWriter.write(document);
xmlWriter.flush();
xmlWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return stringWriter.toString();
}
/**
* 去掉聲明頭的(即<?xml...?>去掉)
* @param document
* @param charset
* @return
*/
public static String documentToStringNoDeclaredHeader(Document document,String charset) {
String xml = documentToString(document, charset);
return xml.replaceFirst("\\s*<[^<>]+>\\s*", "");
}
/**
* 解析XML為Document對象
* @param xml 被解析的XMl
* @return Document
* @throws ParseMessageException
*/
public final static Element parseXml(String xml)throws ParseMessageException{
StringReader sr = new StringReader(xml);
SAXReader saxReader = new SAXReader();
Document document;
try {
document = saxReader.read(sr);
} catch (DocumentException e) {
throw new ParseMessageException(e);
}
Element rootElement = document.getRootElement();
return rootElement;
}
public final static String getText(Element e, String tag){
Element _e = e.element(tag);
if(_e != null)
return _e.getText();
else
return null;
}
public final static String getTextTrim(Element e, String tag){
Element _e = e.element(tag);
if(_e != null)
return _e.getTextTrim();
else
return null;
}
/**
* 獲取節點值.節點必須不能為空,否則拋錯
* @param parent
* @param tag
* @return
* @throws ParseMessageException
*/
public final static String getTextTrimNotNull(Element parent, String tag) throws ParseMessageException{
Element e = parent.element(tag);
if(e == null)
throw new ParseMessageException(parent.getName() + " -> " + tag + " 節點為空.");
else
return e.getTextTrim();
}
/**
* 節點必須不能為空,否則拋錯
* @param parent
* @param tag
* @return
* @throws ParseMessageException
*/
public final static Element elementNotNull(Element parent, String tag) throws ParseMessageException{
Element e = parent.element(tag);
if(e == null)
throw new ParseMessageException(parent.getName() + " -> " + tag + " 節點為空.");
else
return e;
}
public static void main(String[] args) throws PackMessageException, ParseMessageException {
Document document = DocumentHelper.createDocument();
document.setXMLEncoding("GB2312");
Element root = document.addElement("TX");
DomUtil.addChild(root, "REQUEST_SN", "bankBatchSeqId");
DomUtil.addChild(root, "CUST_ID", "cust_id");
DomUtil.addChild(root, "USER_ID", "user_id");
DomUtil.addChild(root, "PASSWORD", "password");
DomUtil.addChild(root, "TX_CODE", "txCode");
DomUtil.addChild(root, "LANGUAGE", "CN");
Element tx_info = root.addElement(Constant.TX_INFO);
DomUtil.addChild(tx_info, Constant.REQUEST_SN1, "request_sn1");
String xml = DomUtil.documentToStringNoDeclaredHeader(root.getDocument(), "GBK");
System.out.println(xml);
root = parseXml(xml);
System.out.println(root.element("TX_INFO").elementText("REQUEST_SN1"));
}
}
主函數測試結果為:
<TX>
<REQUEST_SN>bankBatchSeqId</REQUEST_SN>
<CUST_ID>cust_id</CUST_ID>
<USER_ID>user_id</USER_ID>
<PASSWORD>password</PASSWORD>
<TX_CODE>txCode</TX_CODE>
<LANGUAGE>CN</LANGUAGE>
<TX_INFO>
<REQUEST_SN1>request_sn1</REQUEST_SN1>
</TX_INFO>
</TX>
request_sn1
博客參考文獻:http://www.cnblogs.com/macula/archive/2011/07/27/2118003.html
