package com.sunrise.ocs.webservice.unicom.test;
import java.io.File;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Iterator;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
public class TestDom4j {
private static final Logger log = Logger.getLogger(TestDom4j.class);
private static long bt;
public static void main(String[] args) {
String strXml = "";
int b = 0;
String file1 = "xml/CreateUserRequest.xml";
String file2 = "xml/CancelUserRequest.xml";
if(b==0){
bt = System.currentTimeMillis();
strXml = xmlFile2String(file1);
if (log.isDebugEnabled()) {
log.debug("\nxmlFile2String() use time: "
+ (System.currentTimeMillis() - bt) + " millis\n");
}
}else{
bt = System.currentTimeMillis();
strXml = xmlFile2String2(file1);
if (log.isDebugEnabled()) {
log.debug("\nxmlFile2String2() use time: "
+ (System.currentTimeMillis() - bt) + " millis\n");
}
}
if(b==0){
bt = System.currentTimeMillis();
findElement4XPath1(strXml);
if (log.isDebugEnabled()) {
log.debug("\nfindElement4XPath1() use time: "
+ (System.currentTimeMillis() - bt) + " millis\n");
}
}else{
bt = System.currentTimeMillis();
findElement4XPath2(strXml);
if (log.isDebugEnabled()) {
log.debug("\nfindElement4XPath2() use time: "
+ (System.currentTimeMillis() - bt) + " millis\n");
}
}
}
public static void findElement4XPath1(String xml) {
try {
String str = delNamespace4Pattern(xml);
Document doc = DocumentHelper.parseText(str);
Element e = (Element) doc.selectSingleNode("//CreateUserRequest/RequestMessage/MessageHeader");
if (e != null) {
Iterator iter = e.elementIterator();
while (iter.hasNext()) {
Element sub = (Element) iter.next();
log.debug("\n" + sub.getText() + "\n");
}
}
/* 读取属性的例子
List childNodes = doc.selectNodes("//Config/Child/ChildNode");
for(Object obj:childNodes) {
Node childNode = (Node)obj;
String name = childNode.valueOf("@name"); //读取属性
String text = childNode.getText();
}
*/
} catch (Exception e) {
e.printStackTrace();
}
}
public static void findElement4XPath2(String xml) {
try {
Document doc = DocumentHelper.parseText(xml);
Element root = doc.getRootElement();
HashMap map = new HashMap();
map.put("tns", "http://bme.sunrise.com/unicom/gd");
XPath x = doc.createXPath("//tns:CreateUserRequest/tns:RequestMessage/tns:MessageHeader");
x.setNamespaceURIs(map);
Element e = (Element) x.selectSingleNode(doc);
if (e != null) {
Iterator iter = e.elementIterator();
while (iter.hasNext()) {
Element sub = (Element) iter.next();
if (log.isDebugEnabled()) {
log.debug("\n" + sub.getText() + "\n");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static Document xml2Document(String xml) {
try {
return DocumentHelper.parseText(xml);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String xmlFile2String(String xmlFile) {
try {
return new SAXReader().read(new File(xmlFile)).asXML();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//读取xml文件为xml串
public static String xmlFile2String2(String xmlFile) {
try {
org.w3c.dom.Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlFile);
OutputFormat format = new OutputFormat(document);
//format.setEncoding("UTF-8");
StringWriter stringOut = new StringWriter();
XMLSerializer serial = new XMLSerializer(stringOut, format);
serial.asDOMSerializer();
serial.serialize(document.getDocumentElement());
return stringOut.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static String delNamespace4Pattern(String xml){
String result = "";
try {
result = xml.replaceFirst("xmlns([^ ]*)=([^ ]*)http([^>^\"]*)\"", "");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}