Java USES JDOM to parse XML files

  • 2020-04-01 02:10:46
  • OfStack

JDOM is an open source project that USES pure JAVA techniques to parse, generate, serialize, and manipulate XML documents based on tree structures. JDOM serves JAVA programming directly. It effectively combines the capabilities of SAX and DOM by taking advantage of the many features of the more powerful JAVA language (method overloading, collection concepts, and mapping).
JDOM's official address: (link: http://www.jdom.org/) 1. Create a new interface and two classes to prepare for the future
[Moveable. Java]

package com.njupt.zhb.test;
public interface Moveable {
 void run();
}

[Plane. Java]

package com.njupt.zhb.test;
public class Plane implements Moveable {
 @Override
 public void run() {
  // TODO Auto-generated method stub
  System.out.println(" The plane is flying .....");
 }
}

[" Train ". Java]

package com.njupt.zhb.test;
public class Train implements Moveable{
 @Override
 public void run() {
  System.out.println(" The train is running ....");
 }

}

2. Create a new interface and the main program can call the getBean method to get the corresponding object.

package com.njupt.zhb.test;
public interface BeanFactory {
 Object getBean(String id);
}

3. The XML file to be parsed is as follows:

<?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. Parse the main class of the file and implement the BeanFactory interface.

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>();//To hold the resolved id and object
 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");//Gets all the values under this node
  System.out.println(list.size());
  for (int i = 0; i < list.size(); i++) {
   Element bean = (Element) list.get(i);
   String id = bean.getAttributeValue("id");//Get the value for the id
   String clazz = bean.getAttributeValue("class");//Get the value of the class
   Object o = Class.forName(clazz).newInstance();//Java reflection mechanism that generates objects by class name
   mapContainer.put(id, o);//Save to a map
   System.out.println(id + " " + clazz);
  }
 }
 @Override
 public Object getBean(String id) {
  return mapContainer.get(id);
 }
}

5. Call the main program TestMain.

package com.njupt.zhb.test;
public class TestMain {
 public static void main(String[] args) throws Exception {
  BeanFactory f = new ClassPathXmlApplicationContext(
    "com/njupt/zhb/test/sample.xml");
  Object obj1 = f.getBean("train");//Gets an object labeled train
  Moveable m1 = (Moveable)obj1;//Interface call subclass
  m1.run();
  // //----------------------
  Object obj2 = f.getBean("plane");
  Moveable m2 = (Moveable) obj2;
  m2.run();

 }
}

Experimental results:

2
train com.njupt.zhb.test.Train
plane com.njupt.zhb.test.Plane
 The train is running ....
 The plane is flying .....

(link: http://xiazai.jb51.net/201307/yuanma/JDOM_jb51net.zip)

Related articles: