使用DMO遍历xml文件(将xml文件中要获取的内容存到ArrayList集合中再取出来)(DMO解析)

以下是我要将xml文件中要获取的内容存到ArrayList集合中再取出来的代码,大家也可以不用弄到集合去,我弄到集合也是为了好玩而已,哈哈。。。



import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DOMDemo {
	
	
	public static List<Student> getStudentList(){
		ArrayList<Student> list = new ArrayList<Student>();
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse("a.xml");
			Element rootElement = document.getDocumentElement();
			String schoolMaster = rootElement.getAttribute("schoolMaster");
			Attr schoolName = rootElement.getAttributeNode("schoolName");
			short nodeType = rootElement.getNodeType();
			NamedNodeMap attributes2 = rootElement.getAttributes();
			NodeList childNodes = rootElement.getChildNodes();
			for (int i = 0; i < childNodes.getLength(); i++) {
				Node childNode = childNodes.item(i);
				
				if(childNode!=null&&childNode.getNodeName().equals("student")){
				String nodeName = childNode.getNodeName();
				short nodeType2 = childNode.getNodeType();
				NamedNodeMap attributes = childNode.getAttributes();
				Node idNode = attributes.getNamedItem("id");
				String idNodeName = idNode.getNodeName();
				String idNodeValue = idNode.getNodeValue();
				Node sexNode = attributes.getNamedItem("sex");
				String sexNodeName = sexNode.getNodeName();
				String sexNodeValue = sexNode.getNodeValue();
				String textContent = childNode.getTextContent();
				Student student = new Student(textContent, sexNodeValue, Integer.parseInt(idNodeValue));
				list.add(student);
				}
			}
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	public static void test2(){
		List<Student> studentList = getStudentList();
		for (Iterator<Student> iterator = studentList.iterator(); iterator.hasNext();) {
			Student student=  iterator.next();
			System.out.println("id:"+student.id+"\r\n"+"name:"+student.name+"\r\n"+"sex:"+student.sex+"\r\n"+"---------------------------");
		}
	}
	public static void main(String[] args) {
		test2();
	}
}
class Student {
	String name;
	String sex;
	int id;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Student(String name, String sex, int id) {
		this.name = name;
		this.sex = sex;
		this.id = id;
	}
	
}

我的XML文件命名为a.xml,路径在项目目录下,我的xml文件内容为

<?xml version="1.0" encoding="UTF-8"?>
<students schoolMaster="xiaoxun" schoolName="xun">
    I am content...
<student id="01" sex="女">axun</student>Hi!
<student id="02" sex="男">佳询</student>
Hello!
</students>
运行结果如下:


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值