import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
* Xml工具類.
*
* @author qingwu
* @date 2013-04-26
*/
public class XmlUtils {
public static String XML_HEAD = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
public static String ROOT_BEIGN = "<root>";
public static String ROOT_END = "</root>";
/**************************** dom4j begin ********************************/
/**
* 字符串轉org.dom4j.Document.
*
* @param xml
* xml字符串
* @return org.dom4j.Document文檔對象
* @author qingwu
* @date 2013-6-26 上午10:00:00
*/
public static org.dom4j.Document strToDom4jDoc(String xml) {
try {
return DocumentHelper.parseText(xml);
} catch (DocumentException e) {
throw new CaughtException(e);
}
}
/**
* 文件轉org.dom4j.Document.
*
* @param fileName
* 以“/”開頭,根據工程項目相對路徑讀取
* @return org.dom4j.Document
* @author qingwu
* @date 2013-6-26 上午10:00:00
*/
public static org.dom4j.Document fileToDocument(String fileName) {
InputStream is = XmlUtils.class.getResourceAsStream(fileName);
SAXReader reader = new SAXReader();
Document doc = null;
try {
doc = reader.read(is);
} catch (DocumentException e) {
throw new CaughtException(e);
} finally {
try {
is.close();
} catch (IOException e) {
throw new CaughtException(e);
}
}
return doc;
}
/**
* 輸入流轉org.dom4j.Document
*
* @param is
* 輸入流
* @return org.dom4j.Document文檔對象
* @author qingwu
* @date 2013-6-26 上午10:00:00
*/
public static org.dom4j.Document inputStreamToDom4jDoc(InputStream is) {
try {
SAXReader reader = new SAXReader();
return reader.read(is);
} catch (DocumentException e) {
throw new CaughtException(e);
}
}
/**
* org.dom4j.Document轉字符串.
*
* @param document
* org.dom4j.Document文檔對象
* @return xml字符串
* @author qingwu
* @date 2013-6-26 上午10:00:00
*/
public static String docToStr(org.dom4j.Document document) {
return document.asXML();
}
/**************************** dom4j end ********************************/
/**************************** w3c begin ********************************/
/**
* 字符串轉org.w3c.dom.Document.
*
* @param xml
* xml字符串
* @return org.dom4j.Document文檔對象
* @author qingwu
* @date 2013-6-26 上午10:00:00
*/
public static org.w3c.dom.Document strToW3cDoc(String xml) {
DocumentBuilder builder;
try {
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
return builder.parse(new InputSource(new StringReader(xml)));
} catch (ParserConfigurationException e) {
throw new CaughtException(e);
} catch (SAXException e) {
throw new CaughtException(e);
} catch (IOException e) {
throw new CaughtException(e);
}
}
/**
* 輸入流轉org.w3c.dom.Document.
*
* @param is
* 輸入流
* @return org.dom4j.Document文檔對象
* @author qingwu
* @date 2013-6-26 上午10:00:00
*/
public static org.w3c.dom.Document inputStreamToW3cDoc(InputStream is) {
DocumentBuilder builder;
try {
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
return builder.parse(is);
} catch (ParserConfigurationException e) {
throw new CaughtException(e);
} catch (SAXException e) {
throw new CaughtException(e);
} catch (IOException e) {
throw new CaughtException(e);
}
}
/**
* org.w3c.dom.Document轉字符串.
*
* @param document
* org.dom4j.Document文檔對象
* @return xml字符串
* @author qingwu
* @date 2013-6-26 上午10:00:00
*/
public static String docToStr(org.w3c.dom.Document document) {
String result = null;
if (document != null) {
StringWriter strWtr = new StringWriter();
StreamResult strResult = new StreamResult(strWtr);
TransformerFactory tfac = TransformerFactory.newInstance();
try {
javax.xml.transform.Transformer t = tfac.newTransformer();
t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.setOutputProperty(OutputKeys.METHOD, "xml"); // xml, html,
// text
t.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "4");
t.transform(new DOMSource(document.getDocumentElement()),
strResult);
} catch (Exception e) {
System.err.println("XML.toString(Document): " + e);
}
result = strResult.getWriter().toString();
try {
strWtr.close();
} catch (IOException e) {
throw new CaughtException(e);
}
}
return result;
}
/**
* 獲得org.w3c.dom.Document唯一節點的文本值.
*
* @param doc
* org.w3c.dom.Document文檔對象
* @param nodeName
* 節點名稱(唯一節點)
* @return
* @author qingwu
* @date 2013-6-26 上午10:00:00
*/
public static String getNodeText(org.w3c.dom.Document doc, String nodeName) {
return getNodeText(doc, nodeName, 0);
}
/**
* 獲得org.w3c.dom.Document指定索引節點的文本值.
*
* @param doc
* org.w3c.dom.Document文檔對象
* @param nodeName
* 節點名稱
* @param index
* 數組標誌(從0開始)
* @return
* @author qingwu
* @date 2013-6-26 上午10:00:00
*/
public static String getNodeText(org.w3c.dom.Document doc, String nodeName,
int index) {
NodeList node = doc.getElementsByTagName(nodeName);
if (node.getLength() > index && node.item(index) != null) {
return node.item(index).getTextContent();
}
return null;
}
/**
* 獲得org.w3c.dom.Node節點下指定nodeName節點的文本值.
*
* @param node
* org.w3c.dom.Node
* @param nodeName
* 節點名稱
* @return
* @author qingwu
* @date 2013-6-26 上午10:00:00
*/
public static String getNodeText(org.w3c.dom.Node node, String nodeName) {
org.w3c.dom.Node childNode = getChildNode(node, nodeName);
if (childNode != null) {
return childNode.getTextContent();
}
return null;
}
/**
* 獲得org.w3c.dom.Node節點下指定nodeName節點.
*
* @param node
* org.w3c.dom.Node
* @param nodeName
* 節點名稱
* @return
* @author qingwu
* @date 2013-6-26 上午10:00:00
*/
public static org.w3c.dom.Node getChildNode(org.w3c.dom.Node node,
String nodeName) {
NodeList list = node.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
if (list.item(i).getNodeName().equals(nodeName)) {
return list.item(i);
}
}
return null;
}
/**************************** w3c end ********************************/
/**
* 格式化XML字符串(utf-8編碼).
*
* @param xmlMsg
* @return
*/
public static String formatXml(String xmlMsg) {
return formatXml(xmlMsg, null);
}
/**
* 格式化XML字符串.
*
* @param xmlMsg
* @param encoding
* @return
*/
public static String formatXml(String xmlMsg, String encoding) {
org.dom4j.Document _document = strToDom4jDoc(xmlMsg);
String paramXML = "";
if (encoding == null) {
encoding = "utf-8";
}
try {
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding(encoding);
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLWriter writer = new XMLWriter(out, format);
writer.write(_document);
writer.flush();
writer.close();
paramXML = out.toString(format.getEncoding());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return paramXML;
}
}
org.w3c.dom.Document和org.dom4j.Document常用操作
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.