創建解析XML文件的三種解析方法(二)

用jdom解析

package jdom;

 

import java.io.FileOutputStream;

import java.util.Iterator;

import java.util.List;

import java.util.Random;

 

import org.jdom.Document;

import org.jdom.Element;

import org.jdom.input.SAXBuilder;

import org.jdom.output.Format;

import org.jdom.output.XMLOutputter;

 

publicclass jdom {

    privatestatic String xmlpath = "jdom.xml";

   

    publicstaticvoid main(String[] args) throws Exception{

       //resolving();

       createXml();

    }

   

    //創建XML文件並添加節點

    publicstatic  void createXml() throws Exception{

       //創建document對象

       Document document = new Document();

       //創建根節點

       Element root = new Element("students");

       //將根節點添加到document對象中

       document.addContent(root);

       for(int i = 0 ;i < 20 ; i++){

           //創建根元素節點

           Element student = new Element("student");

           //聲明參數ID

           String id ="";

           //循環生成一個9爲的隨機ID

           for(int j = 0; j< 9 ; j ++ ){

              id += new Random().nextInt(8)+1;

           }

           //設置跟元素節點的屬性

           student.setAttribute("id",id);

           //將根元素節點添加到根節點中

           root.addContent(student);

           //聲明一個姓名的數組

           String nameList[] = {"呂布","趙雲","馬超","張飛","關羽","許褚","孫策","周瑜","夏侯淵","張頜","于禁","黃忠","典韋","曹仁","程普"};   

           String sexList[] = {"",""};

           //創建元素節點

           Element name = new Element("name");

           Element sex = new Element("sex");

           Element age =new Element("age");

           Element phone = new Element("phone");

           //設置根元素節點的文本值

           name.setText(nameList[new Random().nextInt(nameList.length)]);

           sex.setText(sexList[new Random().nextInt(sexList.length)]);

           age.setText(new Random().nextInt(20)+20+"");

           String tel ="";

           for(int k = 0; k< 7 ; k++ ){

              tel += new Random().nextInt(9);

           }

           phone.setText("0756-"+tel);

           //將元素節點添加到根元素節點中

           student.addContent(name);

           student.addContent(sex);

           student.addContent(age);

           student.addContent(phone);

       }

       //設置XML輸出排版

       Format format = Format.getPrettyFormat();

       XMLOutputter  out = new XMLOutputter(format);

       //輸出XML文件

       out.output(document, new FileOutputStream(xmlpath));

    }

   

    //解析XML文檔

    @SuppressWarnings("rawtypes")

    publicstaticvoid resolving() throws Exception{

       //獲取XML解析器

       SAXBuilder builder = new SAXBuilder();

       //獲取document對象

       Document doucment = builder.build(xmlpath);

       //獲取根節點

       Element students = doucment.getRootElement();

       //獲取根元素節點

       List studentList = students.getChildren("student");

       //循環獲取元素文本值,第一種方法

       for(int i = 0 ; i< studentList.size() ; i++ ){

           Element student = (Element)studentList.get(i);

           System.out.println("id = "+student.getAttributeValue("id")+"   name = "

           +student.getChildText("name")+"   sex = "

           +student.getChildText("sex")+"   age = "

           +student.getChildText("age")+"   phone = "

           +student.getChildText("phone"));

       }

      

    System.err.println("\n---------------------------------------------------------------------");

      

       //循環獲取元素文本值,第二種方法

       for(Iterator iter=studentList.iterator();iter.hasNext();){

           Element student = (Element)iter.next();

           System.out.println("id = "+student.getAttributeValue("id")+"   name = "

           +student.getChildText("name")+"   sex = "

           +student.getChildText("sex")+"   age = "

           +student.getChildText("age")+"   phone = "

           +student.getChildText("phone"));

       }

    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章