您的位置:首页 > 编程语言 > Java开发

[java]操作XML

2015-06-19 15:02 337 查看
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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java