创建解析XML文件的三种解析方法(三)

用dom4j解析

package dom4j;

 

import java.io.File;

import java.io.FileWriter;

import java.util.Iterator;

import java.util.List;

import java.util.Random;

 

import org.dom4j.Document;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

import org.dom4j.io.OutputFormat;

import org.dom4j.io.SAXReader;

import org.dom4j.io.XMLWriter;

 

publicclass dom4j {

    privatestatic String xmlpath = "dom4j.xml";

   

    publicstaticvoid main(String[] args)throws Exception{

       //createXml();

       resolving();

    }

   

    publicstaticvoid createXml() throws Exception{

       //创建document对象

       Document document = DocumentHelper.createDocument();

       //创建根节点

       Element root = DocumentHelper.createElement("studnets");

       //将根节点添加到document对象中

       document.setRootElement(root);

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

           //创建根元素节点并将根元素节点添加到根节点中

           Element student = root.addElement("student");

           //设置根元素节点的值

           //声明参数ID

           String id ="";

           //循环生成一个9为的随机ID

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

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

           }

           student.addAttribute("id",id);

           //创建元素节点并将元素节点添加到根元素节点中

           Element name = student.addElement("name");

           Element sex = student.addElement("sex");

           Element age = student.addElement("age");

           Element phone = student.addElement("phone");

           //声明一个姓名的数组

           String nameList[] = {"吕布","赵云","马超","张飞","关羽","许褚","孙策","周瑜","夏侯渊","张颌","于禁","黄忠","典韦","曹仁","程普"};   

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

           //设置元素节点的文本值

           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);

       }

       //设置XML文件输出的格式

       OutputFormat format = new OutputFormat("\t", true);

       format.setEncoding("utf-8");

       XMLWriter xmlWriter = new XMLWriter(new FileWriter(xmlpath), format);

       //输出XML文件

       xmlWriter.write(document);

       xmlWriter.close();

    }

   

    //解析XML文件

    @SuppressWarnings("rawtypes")

    publicstaticvoid resolving() throws Exception{

       //获取XML解析器

       SAXReader saxReader = new SAXReader();

       //获取document对象

       Document document = saxReader.read(new File(xmlpath));

       //获取根节点

       Element root = document.getRootElement();

       //获取根元素节点

       List chindList = root.elements();

       //循环获取元素文本值,第一种方法

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

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

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

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

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

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

           +student.elementText("phone"));

       }

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

       //循环获取元素文本值,第二种方法

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

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

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

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

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

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

           +student.elementText("phone"));

       }

    }

}

你可能感兴趣的:(创建解析XML文件的三种解析方法(三))