java读取xml文件

可以用两种方法实现:
1.利用jdk的dom进行读取

2.利用dom4j进行读取


1.创建xml文件
<?xml version="1.0" encoding="UTF-8"?>
<questions>
	<classname>org.olympics.yizu.Question</classname>
	<question>
		<title>奥林匹克旗环的颜色自左至右为</title>

		<answer>A.蓝黄黑绿红</answer>
		<answer>B.蓝黑黄绿红</answer>
		<answer>C.蓝黄黑红绿</answer>
		<answer>D.蓝黄绿黑红</answer>

		<rightanswer>A</rightanswer>
	</question>
	<question>
		<title>国际奥委会总部设在?</title>

		<answer>A.美国纽约</answer>
		<answer>B.英国伦敦</answer>
		<answer>C.瑞士洛桑</answer>
		<answer>D.法国巴黎</answer>

		<rightanswer>C</rightanswer>
	</question>
	<question>
		<title>伦敦奥运会吉祥物是?</title>

		<answer>A.阿米克</answer>
		<answer>B.文洛克</answer>
		<answer>C.虎多利</answer>
		<answer>D.米查</answer>

		<rightanswer>C</rightanswer>
	</question>
</questions>



2.写一个对象,用来封装读取的数据,xml文件的格式我们可以用一个Question类:
/**
 * 
 * @author 李紅序 2012-08-06
 *  用于保存题目的类
 */
public class Question {
	private String title;

	private String[] questions;

	private String rightAnswer;

	private String count;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String[] getQuestions() {
		return questions;
	}

	public void setQuestions(String[] questions) {
		this.questions = questions;
	}

	public String getRightAnswer() {
		return rightAnswer;
	}

	public void setRightAnswer(String rightAnswer) {
		this.rightAnswer = rightAnswer;
	}

	public String getCount() {
		return count;
	}

	public void setCount(String count) {
		this.count = count;
	}

}


3.遍历xml文件保存在Question中,并返回一个List<Question>

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class ReadXml {
	/**
	 * 遍历xml文档
	 * */
	public static List<Question> queryXml(String path) {
		List<Question> queList = new ArrayList<Question>();
		try {
			// 得到DOM解析器的工厂实例
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			// 从DOM工厂中获得DOM解析器
			DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
			// 声明为File为了识别中文名
			Document doc = null;
			try {
				doc = dbBuilder.parse(path);
			}
			// 得到文档名称为Student的元素的节点列表
			catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			NodeList nList = doc.getElementsByTagName("question");

			// 遍历该集合,显示结合中的元素及其子元素的名字
			for (int i = 0; i < nList.getLength(); i++) {
				Question que = new Question();
				Element node = (Element) nList.item(i);
				que.setTitle(node.getElementsByTagName("title").item(0)
						.getFirstChild().getNodeValue());
				String[] arr = new String[4];
				for (int j = 0; j < 4; j++) {
					arr[j] = node.getElementsByTagName("answer").item(j)
							.getFirstChild().getNodeValue();
				}
				que.setQuestions(arr);
				que.setRightAnswer(node.getElementsByTagName("rightanswer")
						.item(0).getFirstChild().getNodeValue());
				queList.add(que);
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return queList;
	}
}


4.最后写一个main进行测试

public class Test{
  public static void main(String[] args) {
	
      List<Question> queList=ReadXml.readXML(path);
      for(Question question:queList){

       /*此处可以遍历输出question的值*/
       System.out.println(question.getTitle());//输出title
       for (int j = 0; j < 4; j++) {

       System.out.println(question.getQuestions()[i]);//遍历输出question
				}
       System.out.println(question.getRightAnswer());//输出正确答案rightAnswer

      }
  }
}

你可能感兴趣的:(java,jdk,xml)