XML文件内容
?>
id="1000000002">
ename=""/>
ename=""/>
ename=""/>
ename=""/>
ename=""/>
ename=""/>
ename=""/>
ename=""/>
totalPropTypeName="Skill Prop">
ename=""/>
ename=""/>
ename=""/>
ename=""/>
totalPropTypeName="Function Prop">
ename=""/>
ename=""/>
ename=""/>
ename=""/>
ename=""/>
定义JAVA对象类[root]
package org.jskyme.xml.bean;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Element;
public class CONRootElement {
private List propLst = new ArrayList();
public void newRootElement(Element rootElm,
CONRootElement rel) {
List itemLst =
rootElm.elements();
for (int i = 0; i
< itemLst.size(); i++) {
Element elem
= (Element) itemLst.get(i);
if
(elem.getName().equals(XML_GAME)) {
CONGameElement
prope = new CONGameElement();
prope.initPropElement(elem,
rel.propLst, prope);
}
}
}
public List getPropLst() {
return propLst;
}
public void setPropLst(List propLst) {
this.propLst =
propLst;
}
public static String XML_ROOT = "root";
public static String XML_GAME = "Game";
public static String XML_ID = "id";
public static String XML_PROPFILE =
"PropFile";
public static String XML_FILENAME =
"fileName";
public static String XML_TOTALPROPTYPE =
"totalPropType";
public static String XML_TOTALPROPNAME =
"totalPropTypeName";
public static String XML_PROPTYPE =
"PropType";
public static String XML_TYPE = "type";
public static String XML_CNAME =
"cname";
public static String XML_ENAME = "ename";
}
定义JAVA对象类[Game]
package org.jskyme.xml.bean;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Element;
public class CONGameElement {
private String id = "";
private List propFileLst = new
ArrayList();
public void initPropElement(Element elem, List
propLst, CONGameElement prope) {
prope.setId(elem.attribute(CONRootElement.XML_ID).getValue());
List itemLst =
elem.elements();
for (int i = 0; i
< itemLst.size(); i++) {
Element
chidelem = (Element) itemLst.get(i);
if
(chidelem.getName().equals(CONRootElement.XML_PROPFILE))
{
CONPropFileElement
conPropeFile = new CONPropFileElement();
conPropeFile.initPropElement(chidelem,
prope.propFileLst, conPropeFile);
}
}
propLst.add(prope);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
定义JAVA对象类[PropFile]
package org.jskyme.xml.bean;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Element;
public class CONPropFileElement {
private String filename = "";
private String totalPorpType = "";
private String totalPorpTypeName = "";
private List propTypeLst = new ArrayList();
public void initPropElement(Element elem, List
propFileLst,
CONPropFileElement
conPropeFile) {
conPropeFile.setFilename(elem.attribute(CONRootElement.XML_FILENAME)
.getValue());
conPropeFile.setTotalPorpType(elem.attribute(
CONRootElement.XML_TOTALPROPTYPE).getValue());
conPropeFile.setTotalPorpTypeName(elem.attribute(
CONRootElement.XML_TOTALPROPNAME).getValue());
List itemLst =
elem.elements();
for (int i = 0; i
< itemLst.size(); i++) {
Element
chidelem = (Element) itemLst.get(i);
if
(chidelem.getName().equals(CONRootElement.XML_PROPTYPE))
{
CONPropTypeElement
prope = new CONPropTypeElement();
prope
.initPropElement(chidelem,
conPropeFile.propTypeLst,
prope);
}
}
propFileLst.add(conPropeFile);
}
public String getFilename() {
return filename;
}
public void setFilename(String filename)
{
this.filename =
filename;
}
public String getTotalPorpType() {
return totalPorpType;
}
public void setTotalPorpType(String
totalPorpType) {
this.totalPorpType =
totalPorpType;
}
public String getTotalPorpTypeName() {
return
totalPorpTypeName;
}
public void setTotalPorpTypeName(String
totalPorpTypeName) {
this.totalPorpTypeName =
totalPorpTypeName;
}
public List getPropTypeLst() {
return propTypeLst;
}
public void setPropTypeLst(List propTypeLst)
{
this.propTypeLst =
propTypeLst;
}
}
定义JAVA对象类[PropType]
package org.jskyme.xml.bean;
import java.util.List;
import org.dom4j.Element;
public class CONPropTypeElement {
private String type = "";
private String cname = "";
private String ename = "";
public void initPropElement(Element chidelem,
List propTypeLst,
CONPropTypeElement
prope) {
prope.setType(chidelem.attribute(CONRootElement.XML_TYPE).getValue());
prope.setCname(chidelem.attribute(CONRootElement.XML_CNAME).getValue());
prope.setEname(chidelem.attribute(CONRootElement.XML_ENAME).getValue());
propTypeLst.add(prope);
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
}
调用代码示例
package org.jskyme.xml;
import java.io.File;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jskyme.xml.bean.CONGameElement;
import org.jskyme.xml.bean.CONRootElement;
public class XmlFileUnit {
public static void main(String[] args)
{
XmlFileUnit xfu = new
XmlFileUnit();
try {
xfu.getConRootInstan(new
File("D:/job.doc/Propinfo.xml"));
} catch (DocumentException e)
{
e.printStackTrace();
}
}
private void
getConRootInstan(File file) throws DocumentException {
SAXReader reader = new
SAXReader();
Document document =
reader.read(file);
Element rootElm =
document.getRootElement();
CONRootElement rel = new
CONRootElement();
if
(rootElm.getName().equals(CONRootElement.XML_ROOT)) {
rel.newRootElement(rootElm,
rel);
System.out.println((CONGameElement)
rel.getPropLst().get(0));
}
}
}