XML文件解析简单实现

XML文件解析

XML文件解析步骤:
1.四句固定搭配,目的:确定需要解析的文档,以及解析方式
2.以节点列表形式取得二级标签,遍历节点列表
3.获取标签里的属性值
4.将获取的值使用类保存起来

需要解析的文件:

<?xml version="1.0" encoding="UTF-8"?>
<students>
	<student id="00001" name="张小三" sex="" birtdhday="2002-10-3">
		<hobby>游泳</hobby>
		<hobby>跳绳</hobby>
		<hobby>打游戏</hobby>
		<hobby>跑步</hobby>
		<hobby>睡觉</hobby>
		<introduce>
			活泼开朗的小鲜肉!
		</introduce>
	</student>
	<student id="00002" name="朱子玉" sex="" birtdhday="2008-10-21">
		<hobbies>
			<hobby>游泳</hobby>
			<hobby>画画</hobby>
			<hobby>学习</hobby>
			<hobby>古筝</hobby>
		</hobbies>
		<introduce>
			小仙女一枚!
		</introduce>
	</student>
</students>

因上述XML文件中有两种表达信息的形式,属性和标签,所以下面涉及的解析方法也有两种。

方法一:

解析属性中的信息:使用getAttribute(String name)方法获取

String id = student.getAttribute("id");
方法二:

解析标签中的内容:使用getElementsByTagName(String name)方法获取

Element idElement = (Element) student.getElementsByTagName("id");
String id = idElement.getTextContent();

解析过程

public class XMLParseAttribute {
	public static void main(String[] args) {
		try {
			//四句固定搭配,对于这四句话,目前为止,不必深究,过于纠结细节对我们并无好处
			InputStream is = XMLParseAttribute.class.getResourceAsStream("/student.att.xml");
			DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document document =db.parse(is);
			
			//以“结点列表”的形式存储获取到的标签名称
			NodeList studentList = document.getElementsByTagName("student");
			//遍历节点列表
			for(int i=0;i<studentList.getLength();i++) {
				Student stu = new Student();
				
				//通过NodeList类的item(下标)方法,取得节点
				Element student = (Element) studentList.item(i);  
				
				
				
				//方法1:获取属性信息
				String id = student.getAttribute("id");
				String name = student.getAttribute("name");
				String sex = student.getAttribute("sex");
				String birthday = student.getAttribute("birthday");
				//保存值
				stu.setId(id);
				stu.setName(name);
				stu.setSex(sex);
				stu.setBirthday(birthday);
				


				//方法2:获取标签里的内容
				NodeList hobbyList = student.getElementsByTagName("hobbies");
				for(int j=0;j<hobbyList.getLength();j++) {
					Element hobby = (Element) hobbyList.item(j);
					//getTextContent()获取从开始标签到结束标签内所有东西,包括空格、回车、Tab
					String hobbystr = hobby.getTextContent();
					stu.addHobbies(hobbystr);
				}
				
				//获取该标签,并取出里面的值
				//trim()清除空格等
				String introduce = student.getElementsByTagName("introduce").item(0).getTextContent().trim();
				stu.setIntroduce(introduce);
				
				System.out.println(stu);
			}
			
			
			
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}

存储解析结果的类:

import java.util.ArrayList;
import java.util.List;

public class Student {
	private String id;
	private String name;
	private String sex;
	private String birthday;
	private String introduce;
	private List<String> hobbies;//记得·初始化
	
	public Student() {
		hobbies = new ArrayList<>();
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = 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 String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	
	public String getIntroduce() {
		return introduce;
	}

	public void setIntroduce(String introduce) {
		this.introduce = introduce;
	}

	public List<String> getHobbies() {
		return hobbies;
	}

	public void addHobbies(String hobby) {
		if(hobbies.contains(hobby)) {
			return;
		}
		hobbies.add(hobby);
	}

	@Override
	public String toString() {
		StringBuffer str = new StringBuffer();
		str.append(id).append(" ")
		.append(name).append(" ")
		.append(sex).append(" ")
		.append(birthday).append("\n")
		.append("简介:")
		.append(introduce).append("\n")
		.append("爱好:");
		for(String hobby:hobbies) {
			str.append(hobby);
		}
		return str.toString();//stringBuffer的toString()能将该类型转化为String类型
	}
	
	

}

运行结果:

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值