使用的包是domj4
解析:
//由字符串转变为document对象
Document document = DocumentHelper.parseText(xml);
//获取根节点
Element root = document.getRootElement();
//获取根节点的下一个节点的迭代器
Iterator<Element> iterator = root.elementIterator();
//获取根节点的下一个子节点
Element root2 = iterator.next();
//获取controller节点下的所有子节点
Iterator<Element> iterator1 = root2.elementIterator();
//将获取的子节点全部存储在list列表中
List<Element> list = new ArrayList<Element>();
//将action加入list
while(iterator1.hasNext())
{
Element action = iterator1.next();
list.add(action);
String a = action.getName();
System.out.println(a);
}
//遍历得到action列表
for(Element e: list) {
//获取Acion属性
if(e.attributeValue("name").equals("login")) {
String clazz = e.attributeValue("class");
String method = e.attributeValue("method");
//通过反射实例化并执行对象方法
Class claz = Class.forName(clazz);
Object a = claz.newInstance();
Method m = claz.getMethod("handleLogin");
Object result = m.invoke(a);
// System.out.println((String)result + "aaaaaaaaaaaaaaaaaa");
//获取Action下result节点
Iterator<Element> resultIterator = e.elementIterator();
List<Element> resultList = new ArrayList<Element>();
while(resultIterator.hasNext()) {
//迭代,将result加入列表
Element result1 = resultIterator.next();
System.out.println(result1.getName());
resultList.add(result1);
}
for(Element e1: resultList) {
//获取result节点的属性
if(e1.attributeValue("name").equals((String)result)) {
String type = e1.attributeValue("type") ;
String value = e1.attributeValue("value");
System.out.println(type);
}
else if(e1.attributeValue("name").equals("failure")) {
String type = e1.attributeValue("type") ;
String value = e1.attributeValue("value");
System.out.println(type);
}
}
}
else {
System.out.println("不可识别的Action请求");
}
}
生成:
public class LogInterceptor {
Document document;
Element log;
Element action;
File file = new File("D:\\myeclipse_workspace\\123.txt");
public void preAction(String ActionName) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
String SysTime = df.format(new Date()); // 获取当前系统时间,即为访问开始时间
// 测试打印System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
if (!file.exists()) { // 如果该日志文件不存在则创建该文件
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();// 创建文件工厂
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
document = builder.newDocument();
log = document.createElement("log");
// 创建子节点,并设置属性
action = document.createElement("action");
// 为action添加子节点
Element name = document.createElement("name");
name.setTextContent(ActionName);
action.appendChild(name);
Element stime = document.createElement("s-time");
stime.setTextContent(SysTime);
action.appendChild(stime);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void atferAction() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
String SysTime = df.format(new Date()); // 获取当前系统时间,即为访问结束时间
String result = "success"; // 设置返回值
Element etime = document.createElement("e-time");
etime.setTextContent(SysTime);
action.appendChild(etime);
// 返回结果
Element eresult = document.createElement("result");
eresult.setTextContent(result);
action.appendChild(eresult);
// 为根节点添加子节点
log.appendChild(action);
// 将根节点添加到Document下
document.appendChild(log);
// 创建TransformerFactory对象
TransformerFactory tff = TransformerFactory.newInstance();
// 创建Transformer对象
Transformer tf;
try {
// 生成xml
tf = tff.newTransformer();
tf.transform(new DOMSource(document), new StreamResult(file));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}