当前位置:编程学习 > XML/UML >>

利用JDOM解析xml文件

利用JDOM解析xml文件
JDOM是一个开源项目,它基于树型结构,利用纯JAVA的技术对XML文档实现解析、生成、序列化以及多种操作。JDOM直接为JAVA编程服务。它利用更为强有力的JAVA语言的诸多特性(方法重载、集合概念以及映射),把SAX和DOM的功能有效地结合起来。
 1.首先新建一个接口和2个类,为后续做准备

[Moveable.java]


[java]

package com.njupt.zhb.test; 
 
 
public inte易做图ce Moveable { 
    void run(); 

package com.njupt.zhb.test;


public inte易做图ce Moveable {
 void run();
}


[Plane.java]


[java]
package com.njupt.zhb.test; 
 
public class Plane implements Moveable { 
 
    @Override 
    public void run() { 
        // TODO Auto-generated method stub  
        System.out.println("飞机在翱翔....."); 
    } 
 

package com.njupt.zhb.test;

public class Plane implements Moveable {

 @Override
 public void run() {
  // TODO Auto-generated method stub
  System.out.println("飞机在翱翔.....");
 }

}

 

[Train.java]


[java]
package com.njupt.zhb.test; 
 
 
public class Train implements Moveable{ 
 
    @Override 
    public void run() { 
        System.out.println("火车在飞奔...."); 
    } 
     

package com.njupt.zhb.test;


public class Train implements Moveable{

 @Override
 public void run() {
  System.out.println("火车在飞奔....");
 }
 
}
2.新建一个接口,主程序可以调用getBean方法,获得相应的对象。

[java]
package com.njupt.zhb.test; 
 
public inte易做图ce BeanFactory { 
    Object getBean(String id); 

package com.njupt.zhb.test;

public inte易做图ce BeanFactory {
 Object getBean(String id);
}
3.需要解析的xml文件如下:
[html]
<?xml version="1.0" encoding="UTF-8"?> 
<beans> 
    <bean  
        id="train" 
        class="com.njupt.zhb.test.Train"> 
    </bean> 
    <bean 
        id="plane" 
        class="com.njupt.zhb.test.Plane"> 
    </bean> 
</beans> 

<?xml version="1.0" encoding="UTF-8"?>
<beans>
 <bean
     id="train"
     class="com.njupt.zhb.test.Train">
 </bean>
 <bean
     id="plane"
     class="com.njupt.zhb.test.Plane">
 </bean>
</beans>
4.解析文件的主类,实现了BeanFactory接口。
[java]
package com.njupt.zhb.test; 
 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
 
import org.jdom.Document; 
import org.jdom.Element; 
import org.jdom.input.SAXBuilder; 
import org.jdom.xpath.XPath; 
 
public class ClassPathXmlApplicationContext implements BeanFactory { 
    private Map<String, Object> mapContainer = new HashMap<String, Object>();//用于存放解析出来的id和对象  
    public ClassPathXmlApplicationContext(String fileName) throws Exception { 
        SAXBuilder sb = new SAXBuilder(); 
        Document doc = sb.build(this.getClass().getClassLoader() 
                .getResourceAsStream(fileName)); 
        Element root = doc.getRootElement(); 
        List list = XPath.selectNodes(root, "/beans/bean");//获得此节点下的所有值  
        System.out.println(list.size()); 
        for (int i = 0; i < list.size(); i++) { 
            Element bean = (Element) list.get(i); 
            String id = bean.getAttributeValue("id");//获得id对应的值  
            String clazz = bean.getAttributeValue("class");//获得class对应的值  
            Object o = Class.forName(clazz).newInstance();//Java反射机制,根据类名生成对象  
            mapContainer.put(id, o);//保存到map中  
            System.out.println(id + " " + clazz); 
        } 
    } 
 
    @Override 
    public Object getBean(String id) { 
        return mapContainer.get(id); 
    } 
 

package com.njupt.zhb.test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;

public class ClassPathXmlApplicationContext implements BeanFactory {
 private Map<String, Object> mapContainer = new HashMap<String, Object>();//用于存放解析出来的id和对象
 public ClassPathXmlApplicationContext(String fileName) throws Exception {
  SAXBuilder sb = new SAXBuilder();
  Document doc = sb.build(this.getClass().getClassLoader()
    .getResourceAsStream(fileName));
  Element root = doc.getRootElement();
  List list = XPath.selectNodes(root, "/beans/bean");//获得此节点下的所有值
  System.out.println(list.size());
  for (int i = 0; i < list.size(); i++) {
   Element bean = (Element) list.get(i);
   String id = bean.getAttributeValue("id");//获得id对应的值
   String clazz = bean.getAttributeValue("class");//获得class对应的值
   Object o = Class.forName(clazz).newInstance();//Java反射机制,根据类名生成对象
   mapContainer.put(id, o);//保存到map中
   System.out.println(id + " " + clazz);
  }
 }

 @Override
 public Object getBean(String id) {
 

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,