创建解析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"));

}

}

}

相关文章

引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满足人的生产生活需要而产生的。具体...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
http://blog.jobbole.com/79252/ 引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满...
(点击上方公众号,可快速关注) 公众号:smart_android 作者:耿广龙|loonggg 点击“阅读原文”,可查看...
一、xml与xslt 相信所有人对xml都不陌生,其被广泛的应用于数据数据传输、保存与序列化中,是一种极为强...