【Java】XML文件的解析

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");
}

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

猜你喜欢

转载自blog.csdn.net/Z_ChenChen/article/details/82944341