需求:
java读XML文档到一个实体bean中
给定一个XML文档,将所有的节点,封装到一个实体中,然后,传XML文件名到程序,将该文件的内容读到实体bean中,返回实体。
xml文档的内容
<serviceApp id="" lastTime="" serviceAddress='' transfersRuleId='' >
<front id='' frontCode='' frontName=''>
</front>
<node id='' nodeCode='' nodeName='' nodeIp='' userName='' password='' port='' nodeBaseData='' nodeExample='' dataBaseOwer='' >
</node>
<table id='' tableName=''>
</table>
<targeFront frontCode='' frontName=''>
</targeFront>
<targeNode id='' nodeCode='' nodeName='' nodeIp='' userName='' password='' port='' nodeBaseData='' nodeExample='' dataBaseOwer=''>
</targeNode>
<targeTable id='' tableName=''>
</targeTable>
<findSql>
select * from table
</findSql>
<transfersRule id='' ruleName=''>
{}
</transfersRule>
</serviceApp>
根据上面的内容,创建一个实体bean(BO形式),值得注意的是XML中有很多节点和属性,所以实体生成的方式也很多,本人采取的是,根节点Root的所有属性,以String的形式存储。 而根节点的其余子节点全部采用Map<string,string>的形式存储。这样做从XML中读数据的时候,比较方便,直接将其 put 进来就好,后期取值的时候也比较方便,可以直接通过 get(key)完成。
实体bean
import java.util.Map;
public class Appserverbean {
private Map<String, String> front; <span style="white-space:pre"> </span>//front节点
private Map<String, String> node; //node节点
private Map<String, String> targeFront; //targeFront节点
private Map<String, String> targeNode; //targenode节点
private StringBuffer findSql; //findsql节点
private String id; //根节点id属性
private String lastTime; //根节点lasttime属性
private String serviceAddress; //根节点serviceAddress属性
private String siteSave; //根节点siteSave属性
private String useState; //根节点useState属性
private String state; //根节点state属性
private String serviceStrategy; //根节点serviceStrategy属性
private String pastTime; //根节点pastTime属性
public String getSiteSave() {
return siteSave;
}
public void setSiteSave(String siteSave) {
this.siteSave = siteSave;
}
public String getUseState() {
return useState;
}
public void setUseState(String useState) {
this.useState = useState;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getServiceStrategy() {
return serviceStrategy;
}
public void setServiceStrategy(String serviceStrategy) {
this.serviceStrategy = serviceStrategy;
}
public String getPastTime() {
return pastTime;
}
public void setPastTime(String pastTime) {
this.pastTime = pastTime;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLastTime() {
return lastTime;
}
public void setLastTime(String lastTime) {
this.lastTime = lastTime;
}
public String getServiceAddress() {
return serviceAddress;
}
public void setServiceAddress(String serviceAddress) {
this.serviceAddress = serviceAddress;
}
public Map<String, String> getFront() {
return front;
}
public void setFront(Map<String, String> front) {
this.front = front;
}
public Map<String, String> getNode() {
return node;
}
public void setNode(Map<String, String> node) {
this.node = node;
}
public Map<String, String> getTargeFront() {
return targeFront;
}
public void setTargeFront(Map<String, String> targeFront) {
this.targeFront = targeFront;
}
public Map<String, String> getTargeNode() {
return targeNode;
}
public void setTargeNode(Map<String, String> targeNode) {
this.targeNode = targeNode;
}
public StringBuffer getFindSql() {
return findSql;
}
public void setFindSql(StringBuffer findSql) {
this.findSql = findSql;
}
}
/**
* 从xml文件中读取数据
* 返回一个实体bean
* Appserverbean 就是先前生成的实体
* @return
*/
public Appserverbean readXml(String filemane) {
StringBuffer sql = null;
Appserverbean bean = new Appserverbean();
//文件所在文件夹
//FILED_URL=D:/Coding/Iexchange_spance/iexchange/src/main/java/com/cicro/iexchange/common/XMLFileHandling/
//从配置文件中 读取数据, 也可直接给定绝对地址
String fileurl=ConnectFactory.getProperties("FILED_URL");
try {
// 读取并解析XML文档
// SAXReader就是一个管道,用一个流的方式,把xml文件读出来
SAXReader reader = new SAXReader(); // User.hbm.xml表示你要解析的xml文档
<span style="white-space:pre"> </span>//InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("app.xml");
//将xml文件读进来
Document doc = reader.read(new File(fileurl+filemane));
// 获取根节点
Element rootElt = doc.getRootElement();
Attribute attribute = null;
//创建map
Map<String, String> rootmap = new HashMap<String, String>();
//遍历得到根节点内的属性,并加到map中
for ( Iterator i = rootElt.attributeIterator(); i.hasNext(); ) {
<span style="white-space:pre"> </span> attribute = (Attribute) i.next();
<span style="white-space:pre"> </span> rootmap.put(attribute.getName(), attribute.getText());
}
/**
* 得到根节点的属性和其对应的值,
*/
//得到根节点中的属性和属性值
String id = rootmap.get("id");
String lastTime = rootmap.get("lastTime");
String serviceAddress = rootmap.get("serviceAddress");
String siteSave = rootmap.get("siteSave");
String useState = rootmap.get("useState");
String state = rootmap.get("state");
String serviceStrategy = rootmap.get("serviceStrategy");
String pastTime = rootmap.get("pastTime").toString();
//创建一个节点名字集合
List elementnameli = new ArrayList();
//得到下面的所有子节点
for ( Iterator i = rootElt.elementIterator(); i.hasNext(); ) {
Element element = (Element) i.next();
elementnameli.add(element.getName());
}
//遍历节点名字集合
for (int i = 0; i < elementnameli.size(); i++) {
//得到节点的名字
String elementname = (String)elementnameli.get(i);
//如果名字等于findSql,执行这句
if("findSql".equalsIgnoreCase(elementname)){
Element Element = rootElt.element(elementname);
String sql1 = Element.getText().toString();
sql = new StringBuffer(sql1);
bean.setFindSql(sql);
}
if("front".equalsIgnoreCase(elementname)){
//得到节点对象
Element frontElement = rootElt.element(elementname);
//遍历节点获得其中属性
Map<String, String> map2 = new HashMap<String, String>();
for ( Iterator j = frontElement.attributeIterator(); j.hasNext();) <span style="white-space:pre"> <span style="white-space:pre"> </span>{
Attribute Element = (Attribute) j.next();
map2.put(Element.getName(), Element.getText());
bean.setFront(map2);
}
}
if("node".equalsIgnoreCase(elementname)){
//得到节点对象
Element frontElement = rootElt.element(elementname);
//遍历节点获得其中属性
Map<String, String> map2 = new HashMap<String, String>();
for ( Iterator j = frontElement.attributeIterator(); j.hasNext();) <span style="white-space:pre"> <span style="white-space:pre"> </span>{
Attribute Element = (Attribute) j.next();
map2.put(Element.getName(), Element.getText());
bean.setNode(map2);
}
}
if("targeFront".equalsIgnoreCase(elementname)){
//得到节点对象
Element frontElement = rootElt.element(elementname);
//遍历节点获得其中属性
Map<String, String> map2 = new HashMap<String, String>();
for ( Iterator j = frontElement.attributeIterator(); j.hasNext();) <span style="white-space:pre"> <span style="white-space:pre"> </span>{
Attribute Element = (Attribute) j.next();
map2.put(Element.getName(), Element.getText());
bean.setTargeFront(map2);
}
}
if("targeNode".equalsIgnoreCase(elementname)){
//得到节点对象
Element frontElement = rootElt.element(elementname);
//遍历节点获得其中属性
Map<String, String> map2 = new HashMap<String, String>();
for ( Iterator j = frontElement.attributeIterator(); j.hasNext();) <span style="white-space:pre"> <span style="white-space:pre"> </span>{<span style="white-space:pre"> </span>
Attribute Element = (Attribute) j.next();
map2.put(Element.getName(), Element.getText());
bean.setTargeNode(map2);
}
}
}
bean.setId(id);
bean.setLastTime(lastTime);
bean.setServiceAddress(serviceAddress);
bean.setSiteSave(siteSave);
bean.setUseState(useState);
bean.setState(state);
bean.setPastTime(pastTime);
System.out.println(bean.getId());
System.out.println(bean.getLastTime());
System.out.println(bean.getSiteSave());
} catch (DocumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return bean;
}