XML对象序列化(原创)
先看看上面两篇文章:
http://myhard.yesky.com/20030218/1652674.shtml
http://www.donews.net/shanyou/archive/2004/07/12/42356.aspx
然后 自己动手 做做:
XML的解析方法大概有DOM 、SAX、JDOM、DOM4J四种方法
解析这样一个xml文件
address1
address2
address3
实现从XML到Java对象
public class UserBean {
private String userName;
private String age;
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
这是一个标准的JavaBean :默认构造函数 private属性 getXXX setXXX访问属性.
这样就几乎和上面的XML一样了.接下来,我只要定义一个UserBean对象,然后从XML中读出相应的节点值 做setXXX就可以.如果我的UserBean增加了一个属性那这个解析程序不是要重写?当然,为每个JavaBean写一个XML解析程序是愚蠢的.
有没有通用的?什么样的XML都能解析都能组装的.于是我想到了reflect.
用reflect就能解决上面提到的XML解析程序的"专一",让它将良好格式的XML和良好格式的JavaBean连接起来.
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.qgz.bean.UserBean;
public class DOMXMLReader {
//文档对象。
private Document doc;
//XML文件所在的路径。
private String filePath;
//构造函数。
public DOMXMLReader(String filePath) {
this.filePath = filePath;
try {
initXMLDocument();
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException("创建XML文档对象出错。");
}
}
//XML文档对象初始化
public void initXMLDocument() throws Exception {
File xmlFile = new File(filePath);
DocumentBuilderFactory builderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
doc = builder.parse(xmlFile);
}
//解析XML 返回指定Class的对象列表
public ArrayList ParsersXML(Class aimClass, String ObjectTagName)
throws Exception {
NodeList nodeList = doc.getElementsByTagName(ObjectTagName);
ArrayList objectList = new ArrayList();
for (int i = 0; i < attmap =" new" childnodelist =" nodeList.getChildNodes();" p =" 0;" attname =" childNodeList.item(p).getNodeName();" attvalue =" childNodeList.item(p).getFirstChild()" tempclass =" aimClass;" tempobject =" tempClass.newInstance();" methods =" tempClass.getMethods();" i =" 0;" mname =" methods[i].getName();" value =" (String)"> 0) {
try {
initXMLDocument();
} catch (Exception ex) {
throw new RuntimeException("没有被初始化的XML文档对象。");
}
return doc;
} else {
throw new RuntimeException("没有被初始化的XML文档对象。");
}
}
}
public void setDoc(Document doc) {
this.doc = doc;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public static void main(String[] args) throws Exception {
DOMXMLReader reader = new DOMXMLReader("D:/MyXML.xml");
ArrayList objectArray = reader.ParsersXML(UserBean.class, "user");
for (int i = 0; i < user =" (UserBean)">
另外,initXMLDocument() 如果文件是保存在数据库的CLOB对象中。可以使用SAXReader进行读取d得到DOCUMENT.
SAXReader reader = new SAXReader();
try {
Document document = reader.read(XmlHelper.getEncodingXmlReader(
this.modelDef, "gbk"));//this.modelDef为从数据库中的clob查询出来的string.getBytes()
Element modelDef = document.getRootElement().element(
ModelDefConstants.MODEL_DEF);
return modelDef;
} catch (Exception e) {
throw new ParseModelDefException("读取模型定义文件出错!", e);
}
没有评论:
发表评论