[java]操作XML


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.SAXException;

public class XmlUtil {
	private Document document;

	List<Element> elements = new ArrayList<Element>();

	public static XmlUtil getInstance() {
		return new XmlUtil();
	}

	private XmlUtil() {
	}

	public void load(String url) throws Exception {
		FileInputStream in = new FileInputStream(url);
		load(in);
	}

	public void load(File file) throws Exception {
		FileInputStream in = new FileInputStream(file);
		load(in);
	}

	public void load(InputStream in) throws Exception {
		try {
			SAXReader reader = new SAXReader();

			document = reader.read(in);
			this.elements = getAllElements(getRootElement());
		} finally {
			in.close();
		}

	}

	public static Document createDocument() {
		Document document = DocumentFactory.getInstance().createDocument();
		document.setXMLEncoding("GBK");
		return document;
	}

	public List<Element> getAllElements(Element element) {
		List<Element> elements = new ArrayList<Element>();
		if (element.elements().size() == 0) {
			elements.add(element);
			return elements;
		} else {
			elements.add(element);
			for (Object o : element.elements()) {
				Element e = (Element) o;
				elements.addAll(getAllElements(e));
			}
			return elements;
		}
	}

	public List findByPath(String path) {
		return getRootElement().elements(path);
	}

	public Element findById(String id) {
		List<Element> ele = findByAttribute("id", id);
		if (ele.size() > 0)
			return ele.get(0);
		return null;
	}

	public List<Element> findByAttribute(String attrName, String attrValue) {
		List<Element> ele = new ArrayList<Element>();
		for (Element e : this.elements) {
			Attribute attr = e.attribute(attrName);
			if (attr != null && attrValue.equals(attr.getValue())) {
				ele.add(e);
			}
		}
		return ele;
	}

	public List<Element> getElements() {
		return elements;
	}

	public void setElements(List<Element> elements) {
		this.elements = elements;
	}

	public int findElementIndex(Element e) {
		int index = this.elements.indexOf(e);
		return index > 0 ? index - 1 : index;
	}

	public void updateElement(Element element, int index) {
		Element e = this.elements.get(index);
		this.elements.set(index, element);
		removeElement(index);
		e.getParent().add(element);
	}

	public void addElement(Element parentElement, Element e) {
		parentElement.add(e);
		this.elements.add(e);
	}

	public boolean removeElement(Element e) {

		this.elements.remove(e);
		return e.getParent().remove(e);
	}

	public boolean removeElement(int index) {
		this.elements.remove(index);
		Element e = this.elements.get(index);
		return e.getParent().remove(e);

	}

	public Element getRootElement() {
		return document.getRootElement();
	}

	public String getEncoding() {
		return document.getXMLEncoding();
	}

	public void setEncoding(String encoding) {
		document.setXMLEncoding(encoding);
	}

	public Document getDocument() {
		return document;
	}

	public void setDocument(Document document) {
		this.document = document;
		this.elements = getAllElements(getRootElement());
	}

	public void save(String url) throws SAXException, IOException {
		FileOutputStream out = new FileOutputStream(url);
		save(out);
		out.close();
	}

	public void save(File file) throws SAXException, IOException {
		FileOutputStream out = new FileOutputStream(file);
		save(out);
		out.close();
	}

	public void save(OutputStream out) throws SAXException, IOException {
		out.write(document.asXML().getBytes());
	}

	public void dispose() {
		this.elements.clear();
	}
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值