【JavaSE】XML文件的解析(工具思想)

       先建立一个XML文件。

<?xml version="1.0" encoding="UTF-8"?>
<students>
	<student id = "00001" name = "张三" sex = "男" birth = "2001-5-13">
		<hobby>打篮球</hobby>
		<hobby>扣手机</hobby>
		<hobby>爬山</hobby>
		<hobby>跑步</hobby>
		<hobby>追剧</hobby>
		<hobby>喝茶</hobby>
	</student>
	<student id = "00002" name = "李五" sex = "女" birth = "1998-7-13">
		<hobby>踢足球</hobby>
		<hobby>散步</hobby>
		<hobby>追星</hobby>
		<hobby>嗑瓜子</hobby>
	</student>
</students>

       首先,先对XML文件用基本的方式进行解析。

public static void main(String[] args) {
	try {
			try {
				InputStream is = Test.class.getResourceAsStream("/student.att.xml");
				DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
				Document document = db.parse(is);
				NodeList studentList = document.getElementsByTagName("student");

				for (int i = 0;i < studentList.getLength();i++) {
					Element studentElement = (Element) studentList.item(i);
					
					String id = studentElement.getAttribute("id");
					String name = studentElement.getAttribute("name");
					String sex = studentElement.getAttribute("sex");
					String birthday = studentElement.getAttribute("birth");
					
					System.out.println("id:" + id + "  name:" + name + "  sex:" + sex + "  birth:" + birthday );
					NodeList hobbies = studentElement.getElementsByTagName("hobby");
					for (int j = 0;j < hobbies.getLength();j++) {
						Element hobbyElement = (Element) hobbies.item(j);
						String hobby = hobbyElement.getTextContent();
						System.out.println("hobby:" + hobby);
					}
					System.out.println();
				}
			} catch (SAXException | IOException e) {
				e.printStackTrace();
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
}

       结果如下:
在这里插入图片描述
现在,单独写一个工具,

import java.io.IOException;
import java.io.InputStream;

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

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

public abstract class XMLParser {
	//只需一份就够
	public static final DocumentBuilderFactory dbf;
	public static DocumentBuilder db;
	
	static {
		dbf = DocumentBuilderFactory.newInstance();
		try {
			db = dbf.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	}
	
	public XMLParser() {
	}
	
	// 获取Document,Document是w3c的,不要导错了
	public static Document getDocument(InputStream is) {
		Document document = null;
		if (is == null) {
			return document;
		}
		try {
			document = db.parse(is);
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return document;
	}
	
	public static Document getDocument(String xmlPath) {
		InputStream is = Class.class.getResourceAsStream(xmlPath);
		if (is == null) {
			System.out.println("路径 :" + xmlPath + "不存在");
			return null;
		}
		return getDocument(is);
	}
	
	// 抽象方法,由用户自行处理得到的标签
	public abstract void dealElement(Element element,int index);
	
	// 抽象方法在for循环中,每次读取到一个标签,就交给抽象方法,由用户自行使用
	public void parseTag(Document document,String tagName) {
		NodeList nodeList = document.getElementsByTagName(tagName);
		for (int i = 0;i < nodeList.getLength();i++) {
			Element element = (Element) nodeList.item(i);
			dealElement(element,i);
		}
	}
	public void parseTag(Element ele,String tagName) {
		NodeList nodeList = ele.getElementsByTagName(tagName);
		for (int i = 0;i < nodeList.getLength();i++) {
			Element element = (Element) nodeList.item(i);
			dealElement(element,i);
		}
	}
}

       结果如下:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值