⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 specialxmlreader.java

📁 一个用java开发的具有搜索功能的图书管理系统
💻 JAVA
字号:
package library;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.ProcessingInstruction;
import org.jdom.Text;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;

public class SpecialXMLReader {

	private String fileName=null;
	
	public static SpecialXMLReader specialXMLReader=null;
	
	private Document doc = null;
	
	private SAXBuilder builder = null;
	
	
	private SpecialXMLReader(String fileName) throws IOException, JDOMException
	{
		this.fileName=fileName;
		checkFile();
		builder = new SAXBuilder();
		doc = builder.build(new File(fileName));
	}
	
	public List getList(String xpath) throws JDOMException, IOException
	{
        XPath xp=XPath.newInstance(xpath);
		return xp.selectNodes(doc);
	}
	public String getName(String xpath) throws JDOMException
	{
		XPath xp=XPath.newInstance(xpath);
		Object obj=xp.selectSingleNode(doc);
		return ((Element)obj).getName();
	}
	public String getValue(String xpath) throws JDOMException
	{
		XPath xp=XPath.newInstance(xpath);
		Object obj=xp.selectSingleNode(doc);
		if(obj instanceof Element)
		{
			return ((Element)obj).getTextTrim();
		}
		else
		{
			if(obj instanceof Attribute)
			{
				return ((Attribute)obj).getValue();
			}
			else
			{
				if(obj instanceof Text)
				{
					return ((Text)obj).getText();
				}
				else
				{
					if(obj instanceof org.jdom.CDATA)
					{
						return ((org.jdom.CDATA)obj).getText();
					}
					else
					{
						if(obj instanceof ProcessingInstruction)
						{
							return ((ProcessingInstruction)obj).getData();
						}
						else
						{
							return null;
						}
					}
				}
			}
		}
	}
	
	public String[] getElementsValues(String xpath) throws JDOMException
	{
		XPath xp=XPath.newInstance(xpath);
		List list=xp.selectNodes(doc);
		Iterator iterator=list.iterator();
		LinkedList<String> linkList=new LinkedList<String>();
		while(iterator.hasNext())
		{
			Element element=(Element)iterator.next();
			linkList.add(element.getText());
		}
		String[] values=new String[linkList.size()];
		for(int i=0;i<values.length;i++)
		{
			values[i]=linkList.get(i);
		}
		return values;
		
	}
	
	private void checkFile() throws IOException
	{
		File file=new File(fileName);
		if(file.exists()==false)
		{
			throw new FileNotFoundException();
		}
		else
		{
			if(file.isDirectory())
			{
				throw new IOException("参数错误 : 你设置的XML文件路径不是正确的路径");
			}
			else
			{
				System.out.println("XML文件路径正确");
			}
		}
	}
	
	synchronized public static SpecialXMLReader getInstance(String fileName) throws IOException, JDOMException
	{
		if(specialXMLReader==null)
		{
			specialXMLReader=new SpecialXMLReader(fileName);
		}
		return specialXMLReader;
	}
	
	public static SpecialXMLReader getInstance() throws Exception
	{
		if(specialXMLReader==null)
		{
			throw new Exception("你还没有设置XML文件路径,请先调用getInstance(fileName)方法进行初始化");
		}
		else
		{
			return specialXMLReader;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -