使用JDOM解析xml
程序员文章站
2022-05-28 08:01:10
...
首先要导入第三方jar包: jdom.jar
package my.yaner;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Properties;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class Utils {
/**解析一个xml文件, 生成JDOM模型*/
public static Document loadDOM(InputStream is) throws Exception {
SAXBuilder sb = new SAXBuilder();
sb.setValidation(false);
Document doc = sb.build(is);
return doc;
}
/**解析一个xml文件,生成JDOM模型*/
public static Document loadDOM(File file) throws Exception {
SAXBuilder sb = new SAXBuilder();
sb.setValidation(false);
Document doc = sb.build(file);
return doc;
}
/**载入数据1. */
public void load(File dir) throws Exception {
//载入配置
Properties config = new Properties();
Document configDoc = loadDOM(new File(dir, "config.xml"));
List list = configDoc.getRootElement().getChildren();
for(int i = 0; i < list.size(); i++) {
if(list.get(i) instanceof Element){
Element elem = (Element) list.get(i);
String configName = elem.getName();
String configValue = elem.getTextTrim();
config.setProperty(configName, configValue);
}
}
}
/**载入数据2. */
public void loadResourceVersions() throws Exception {
File baseDir = new File("F:/dbtest");
Document doc = Utils.loadDOM(new File(baseDir, "fileversion.xml"));
List list = doc.getRootElement().getChildren("file");
Hashtable<String, Integer> ret = new Hashtable<String, Integer>();
for (Object obj : list) {
Element elem = (Element)obj;
String fileName = elem.getAttributeValue("path");
int fileVersion = Integer.parseInt(elem.getAttributeValue("version"));
ret.put(fileName, fileVersion);
}
}
/** 把一个JDOM模型保存到XML文件中 */
public static void saveDOM(Document doc, File file, boolean addSpace) throws Exception{
FileOutputStream fos = null;
String encoding = System.getProperty("pip_xml_encoding");
if (encoding == null) {
encoding = "GBK";
}
try{
XMLOutputter out;
if (!addSpace) {
// 删除所有无用的空格
ArrayList stack = new ArrayList();
stack.add(doc.getRootElement());
while (stack.size() > 0) {
Object obj = stack.remove(0);
if (obj instanceof Element) {
Element element = (Element)obj;
List list = element.getMixedContent();
for (int i = 0; i < list.size(); i++) {
Object child = list.get(i);
if (child instanceof String && ((String)child).trim().isEmpty()) {
list.remove(i);
i--;
} else if (child instanceof Element) {
stack.add(child);
}
}
}
}
}
out = new XMLOutputter(" ", true, encoding);
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
out.output(doc, bos);
bos.flush();
}catch(Exception e){
throw e;
}finally{
if(fos != null){
try{
fos.close();
}catch(IOException e){
}
}
}
}
/** 把一个JDOM模型保存到XML文件中 */
public static void saveDOM(Document doc, OutputStream os) throws Exception{
try{
String encoding = System.getProperty("pip_xml_encoding");
if (encoding == null) {
encoding = "GBK";
}
XMLOutputter out = new XMLOutputter(" ", true, encoding);
BufferedOutputStream bos = new BufferedOutputStream(os);
out.output(doc, bos);
bos.flush();
} catch(Exception e) {
throw e;
}
}
}
上一篇: MATS使用说明