Java uses jdom to parse XML files

Source: Internet
Author: User
Tags xpath
How do you parse an XML file using Jdom in Java? The following small series on the example for you to introduce a detailed. Need friends can refer to the next

Jdom is an open source project that uses pure Java technology to parse, generate, serialize, and manipulate XML documents based on a tree-structured architecture. Jdom directly for Java programming services. It leverages the many features of the more powerful Java language (method overload, set concepts, and mappings) to effectively combine the functionality of Sax and DOM.
Jdom's official address:http://www.jdom.org/
1. First create a new interface and 2 classes to prepare for follow-up
[Moveable.java]

Copy Code code as follows:


package com.njupt.zhb.test;


public interface Moveable {


void Run ();


}


[Plane.java]

Copy Code code as follows:


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 soaring ...");


 }


}


[Train.java]

Copy Code code as follows:


package com.njupt.zhb.test;


public class Train implements moveable{


@Override


public void Run () {


System.out.println ("The Train is flying ...");


 }





}


2. Create a new interface, the main program can call the Getbean method, get the corresponding object.

Copy Code code as follows:


package com.njupt.zhb.test;


public interface Beanfactory {


Object Getbean (String ID);


}


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

Copy Code code 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. Parsing the main class of the file, implements the Beanfactory interface.

Copy Code code as follows:


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 store parsed IDs and objects


public Classpathxmlapplicationcontext (String fileName) throws Exception {


Saxbuilder sb = new Saxbuilder ();


Document doc = Sb.build (This.getclass (). getClassLoader ()


. getResourceAsStream (FileName));


Element root = doc.getrootelement ();


list = Xpath.selectnodes (Root, "/beans/bean");//Get 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 corresponding to the ID


String clazz = Bean.getattributevalue ("class");//Get class corresponding value


object o = Class.forName (clazz). newinstance ();//java reflection mechanism, generate object based on class name


mapcontainer.put (ID, O);//Save in Map


System.out.println (id + "" + clazz);


}


 }


@Override


public Object Getbean (String id) {


return Mapcontainer.get (ID);


 }


}


5. Main program Testmain call.

Copy Code code as follows:


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");//Get Label train objects


Moveable M1 = (moveable) obj1;//interface invoke subclass


M1.run ();


  // //----------------------


Object obj2 = F.getbean ("plane");


Moveable m2 = (moveable) obj2;


M2.run ();





 }


}


Experimental Results:

Copy Code code as follows:


2


Train Com.njupt.zhb.test.Train


plane Com.njupt.zhb.test.Plane

The
train is flying ...


plane is flying ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.