【Java】XML文件的解析

本文介绍了一个用于解析XML文件的Java类实现,通过继承和抽象方法实现了对特定标签的处理,展示了如何读取XML文件并遍历节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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 XMLReader {
	// 只需要一份 documentBuilder
	private static DocumentBuilder documentBuilder;
	
	static {
		try {
			documentBuilder = DocumentBuilderFactory
					.newInstance()
					.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	}
	
	public XMLReader() {
	}
	
	// 获取Document,Document是w3c的,不要导错了
	public static Document openDocument(InputStream is) {
		Document document = null;
		try {
			document = documentBuilder.parse(is);
		} catch (SAXException | IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return document;
	}

	public static Document openDocument(String xmlPath) {
		InputStream is = XMLReader.class.getResourceAsStream(xmlPath);
		if (is == null) {
			try {
				// 自定义异常 
				throw new XMLFileNotExistException("文件[" + xmlPath+ "]不存在!");
			} catch (XMLFileNotExistException e) {
				e.printStackTrace();
			}
		} 
		
		return openDocument(is);
	}
	
	public static Document openDocument(File path) {
		InputStream is = null;
		try {
			is = new FileInputStream(path);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return openDocument(is);
	}
	
	// 抽象方法,由用户自行处理得到的标签
	public abstract void dealElment(Element element, int index);
	
	// 抽象方法在for循环中,每次读取到一个标签,就交给抽象方法,由用户自行使用
	public XMLReader parse(Element element, String tagname) {
		NodeList nodeList = element.getElementsByTagName(tagname);
		
		for (int index = 0; index < nodeList.getLength(); index++) {
			Element ele = (Element) nodeList.item(index);
			
			dealElment(ele, index);
		}
		
		return this;
	}
	
	public XMLReader parse(Document document, String tagname) {
		NodeList nodeList = document.getElementsByTagName(tagname);
		
		for (int index = 0; index < nodeList.getLength(); index++) {
			Element element = (Element) nodeList.item(index);
			
			dealElment(element, index);
		}
		
		return this;
	}
	
}

这是我们的XML文件,对其进行解析

<?xml version="1.0" encoding="UTF-8"?>
<Test>
	<class name = "第一个标签" type = "firstLable">
		<parameter id = "one" value = "一"></parameter>
		<parameter id = "two" value = "二"></parameter>
		<parameter id = "thr" value = "三"></parameter>
	</class>
	<class name = "第二个标签" type = "secondLable">
		<parameter id = "four" value = "四"></parameter>
		<parameter id = "six" value = "五"></parameter>
	</class>
</Test>
public static void main(String[] args) {
	new XMLReader() {
		
		@Override
		public void dealElment(Element element, int index) {
			String name = element.getAttribute("name");
			String type = element.getAttribute("type");
			
			System.out.println("name=" + name + "  type=" + type);
			
			new XMLReader() {
				
				@Override
				public void dealElment(Element element, int index) {
					String id = element.getAttribute("id");
					String value = element.getAttribute("value");
					
					System.out.println("\tid=" + id + "  value=" + value);
				}
			}.parse(element, "parameter");
		}
	}.parse(XMLReader.openDocument("/test.xml"), "class");
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值