XmlParser.java
上传用户:damzkj
上传日期:2022-01-07
资源大小:24k
文件大小:1k
- 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;
- }
-
- }