使用xml进行java解析之Dom

<?xml version="1.0" encoding="utf-8"?>
<students>
  <student stuno="1001">
     <name>qingzhiyu</name>
	 <age>22</age>
	 <address>北京</address>
  </student>
  <student stuno="1002">
     <name>zhouxiaoqing</name>
	 <age>20</age>
	 <address>厦门</address>
  </student>
</students>

package javaxml;

public class Student {
  private String stuno;
  private String name;
  private String address;
  private String age;

/**
 * @return the stuno
 */
public String getStuno() {
	return stuno;
}
/**
 * @param stuno the stuno to set
 */
public void setStuno(String stuno) {
	this.stuno = stuno;
}
/**
 * @return the name
 */
public String getName() {
	return name;
}
/**
 * @param name the name to set
 */
public void setName(String name) {
	this.name = name;
}
/**
 * @return the address
 */
public String getAddress() {
	return address;
}
/**
 * @param address the address to set
 */
public void setAddress(String address) {
	this.address = address;
}
/**
 * @return the age
 */
public String getAge() {
	return age;
}
/**
 * @param age the age to set
 */
public void setAge(String age) {
	this.age = age;
}
@Override
	public String toString() {
		String str="stuno="+stuno+",name="+name+",age="+age+",address="+address;
		return str;
	}
  
}

package javaxml;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
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 DOMXMLUtil {

	public static void main(String[] args) throws Exception {
		List<Student>students=new ArrayList<>();
//		对xml文件进行读取操作
		InputStream inputStream=DOMXMLUtil.class.getClassLoader().getResourceAsStream("student.xml");
//		实例化解析工厂
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
//		建立解析器对象
			DocumentBuilder builder=factory.newDocumentBuilder();
//		对xml对象进行解析并获取解析文档
			Document document = builder.parse(inputStream);
//		获取根节点对象
			Element root=document.getDocumentElement();
			System.out.println("根节点对象的名字:"+root.getTagName());
//		获取根节点students当中的全部名字为student的子节点对象
			NodeList nodeList=root.getElementsByTagName("student");
			Student student=null;
//		开始对所获取到的子节点集合对象进行遍历
			for(int i=0;i<nodeList.getLength();i++)
			{
				student=new Student();
//		开始获取节点集合当中的第i个结点对象(student结点对象)
				Element studentNode=(Element) nodeList.item(i);
//				System.out.println(studentNode.getTagName());
//		获取当前student结点对象当中的属性值stuno
				String stuno=studentNode.getAttribute("stuno");
//		获取student结点对象当中的name子节点对象,name结点对象当中的内容是以数组的形式串值过来的
				Element nameNode=(Element)studentNode.getElementsByTagName("name").item(0);
//		获取name结点对象当中的内容值
				String name=nameNode.getTextContent();
				System.out.println("name="+name);
//				获取student结点对象当中的name子节点对象
				Element ageNode=(Element) studentNode.getElementsByTagName("age").item(0);
//				获取name结点对象当中的内容值
				String age=ageNode.getTextContent();
				System.out.println("age="+age);
//				获取student结点对象当中的name子节点对象
				Element addressNode=(Element) studentNode.getElementsByTagName("address").item(0);
//				获取name结点对象当中的内容值
				String address=addressNode.getTextContent();
				
				student.setStuno(stuno);
				student.setName(name);
				student.setAge(age);
				student.setAddress(address);
				students.add(student);
			}
		for (Student student2 : students) {
			System.out.println(student2);
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_34970891/article/details/80766768