如何在java中使用jdom生成和解析xml文件


在java中,可以使用org.jdom*这个包来完成生成.xml文件的任务,下面来通过一个我自己写的例子来简单介绍一下这个包里面一些类的基本使用:

 1 import java.io.FileOutputStream;  
 2 import java.io.IOException; 
//导入org.jdom的一些类
3 import org.jdom2.Document; 4 import org.jdom2.Element; 5 import org.jdom2.JDOMException; 6 import org.jdom2.output.Format; 7 import org.jdom2.output.XMLOutputter; 8 9 public class XMLGetter { 10 11 public void BuildXMLDoc(int number, String ipAddress[], String testAddress[]) throws IOException, JDOMException { 12 13 Element equipments = new Element("equipments");//建立根节点 14 Document doc = new Document(equipments);//用把根节点创建一个Document 15 16 Element equipmentsNum = new Element("equipments-number");//创建根节点下的子节点 17 equipmentsNum.setText(Integer.toString(number)); 18 equipments.addContent(equipmentsNum);//把该子节点加入父节点 19 //循环创建子节点,并加入到父节点中 20 for (int i = 1; i <= number; i++) { 21 Element equipment = new Element("equipment"); 22 equipment.setAttribute("id", "" + i);//添加节点属性并赋值 23 24 Element loginInfo = new Element("login-info"); 25 loginInfo.addContent(new Element("address").setText(ipAddress[i-1])); //创建节点,给节点赋值,然后加入到他的父节点 26 loginInfo.addContent(new Element("telnet-password").setText("CISCO")); 27 loginInfo.addContent(new Element("enable-password").setText("CISCO")); 28 equipment.addContent(loginInfo); 29 30 Element testCommands = new Element("test-commands"); 31 testCommands.addContent(new Element("command").setText("show ip route")); 32 equipment.addContent(testCommands); 33 34 Element testReturn = new Element("test-returns"); 35 testReturn.addContent(new Element("return").setText(testAddress[i-1])); 36 equipment.addContent(testReturn); 37 38 equipments.addContent(equipment); 39 } 40 41 XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());//new一个XMLOutputter,采用PrettyFormat格式(生成的XML可以自动换行) 42 xmlOut.output(doc, new FileOutputStream("autoTest.xml"));//将生成的XML文件输出到文件 43 } 44 45 public static void main(String args[]){ 46 try { 47 XMLGetter xmlGetter = new XMLGetter(); 48 String[] ipAddress = {"172.16.0.1","172.16.0.2"}; 49 String[] testAddress = {"192.168.1.0/24","192.168.2.0/24"}; 50 System.out.println("生成 mxl 文件..."); 51 xmlGetter.BuildXMLDoc(2, ipAddress, testAddress); 52 } catch (Exception e) { 53 e.printStackTrace(); 54 } 55 } 56 }

运行结果:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <equipments>
 3   <equipments-number>2</equipments-number>
 4   <equipment id="1">
 5     <login-info>
 6       <address>172.16.0.1</address>
 7       <telnet-password>CISCO</telnet-password>
 8       <enable-password>CISCO</enable-password>
 9     </login-info>
10     <test-commands>
11       <command>show ip route</command>
12     </test-commands>
13     <test-returns>
14       <return>192.168.1.0/24</return>
15     </test-returns>
16   </equipment>
17   <equipment id="2">
18     <login-info>
19       <address>172.16.0.2</address>
20       <telnet-password>CISCO</telnet-password>
21       <enable-password>CISCO</enable-password>
22     </login-info>
23     <test-commands>
24       <command>show ip route</command>
25     </test-commands>
26     <test-returns>
27       <return>192.168.2.0/24</return>
28     </test-returns>
29   </equipment>
30 </equipments>

下面介绍一下如何利用org.jdom*这个包来解析xml文件,也是通过一个例子来演示如何把上面得到的xml文件解析成信息形式:

 1 import java.io.FileOutputStream;  
 2 import java.io.IOException;  
 3 import java.util.Iterator;  
 4 import java.util.List;  
 5 //下面是引用到JDOM中的类  
 6 import org.jdom2.Document;  
 7 import org.jdom2.Element;  
 8 import org.jdom2.JDOMException;  
 9 import org.jdom2.input.SAXBuilder;  
10 import org.jdom2.output.XMLOutputter;
11 
12 public class XMLParse {
13 
14     public void parseXML(String xmlPath){
15         SAXBuilder builder = new SAXBuilder();//选用jdom中的SAXBuilder解析器解析xml16         try{
17             Document doc = builder.build(xmlPath);//从传入xml文件中提取出doc
18             Element equipments = doc.getRootElement();//从doc中得到根节点,赋值给equipments
19             List equipmentList = equipments.getChildren("equipment");//在equipments中得到名字为equipment的子节点List
20             for(Iterator iter = equipmentList.iterator(); iter.hasNext();){//循环List
21                 Element equipment = (Element)iter.next();
22                 String id = equipment.getAttributeValue("id");//得到equipment的属性id,并把它的值赋值给字符串
23                 String ipAddress = equipment.getChild("login-info").getChild("address").getText();//两重子节点的Text赋值给字符串
24                 String testAddress = equipment.getChild("test-returns").getChild("return").getText();
25                 System.out.println(id + "    " + ipAddress + "    " + testAddress);
26             }
27         
28         XMLOutputter outputter=new XMLOutputter();  //保存Document的修改到XML文件中  
29         outputter.output(doc,new FileOutputStream(xmlPath)); 
30             
31         }catch (JDOMException e) {  
32              e.printStackTrace();  
33         }catch (IOException e) {  
34              e.printStackTrace();  
35         }
36     }
37     
38     public static void main(String args[]){
39         XMLParse xmlParse = new XMLParse();
40         xmlParse.parseXML("autoTest.xml");
41     }
42 }

 控制台输出的结果:


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM