?? xmlparser.java
字號:
package jxtamessenger.xml;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.logging.Logger;
import org.apache.commons.lang.ClassUtils;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class XmlParser {
private static final Logger LOG = Logger.getLogger(XmlParser.class.getName());
@SuppressWarnings("unchecked")
public static Object getObject(String xml) {
Object o = null;
try {
Document document = DocumentHelper.parseText(xml);
Element root = document.getRootElement();
try {
Class c = ClassUtils.getClass(root.attributeValue("class"));
o = c.newInstance();
for (Iterator i = root.elementIterator(); i.hasNext(); ) {
Element element = (Element) i.next();
Method method = c.getDeclaredMethod("set" + StringUtils.capitalize(element.getName()), new Class[] {String.class});
method.invoke(o, element.getText());
}
return o;
} catch (Exception e) {
LOG.warning("Class initialize failed!");
e.printStackTrace();
return null;
}
} catch (DocumentException e) {
LOG.warning("getOnlineMsg() failed");
e.printStackTrace();
}
return null;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -