jdom 封装类。解析、读取、修改、写入(主要方法)。节点可以无限扩展。
答案:1 悬赏:80 手机版
解决时间 2021-01-14 02:45
- 提问者网友:贪了杯
- 2021-01-13 18:00
jdom 封装类。解析、读取、修改、写入(主要方法)。节点可以无限扩展。
最佳答案
- 五星知识达人网友:煞尾
- 2021-01-13 18:53
package com.jbp.xml;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.XPath;
public class XmlDbUntl {
private static Document document = null;
protected static File readFile(File file) throws IOException {
try {
if (file.exists()) {
return file;
}
} catch (Exception e) {
throw new IOException("找不到文件" + e.getMessage());
}
throw new IOException("找不到文件");
}
public static boolean appendElement(File file,Element element,String express){
try{
List list=XPath.newInstance(express).selectNodes(fileToElement(file));
}catch (Exception e) { }
return false;
}
public static Document fileToDocument(File file) throws JDOMException {
try {
document = new SAXBuilder().build(readFile(file));
return document;
} catch (JDOMException e) {
throw e;
} catch (IOException e) {
throw new JDOMException("Can't find ["+file.getPath()+"]file !");
}
}
@SuppressWarnings("unchecked")
public static List queryToElementList(File file, String express)
throws JDOMException {
return XPath.newInstance(express).selectNodes(fileToElement(file));
}
public static Element fileToElement(File file) throws JDOMException{
return fileToDocument(file).getRootElement();
}
@SuppressWarnings("unchecked")
public static List fileToElementList(File file)
throws JDOMException {
return fileToElement(file).getChildren();
} public static List
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.XPath;
public class XmlDbUntl {
private static Document document = null;
protected static File readFile(File file) throws IOException {
try {
if (file.exists()) {
return file;
}
} catch (Exception e) {
throw new IOException("找不到文件" + e.getMessage());
}
throw new IOException("找不到文件");
}
public static boolean appendElement(File file,Element element,String express){
try{
List
}catch (Exception e) { }
return false;
}
public static Document fileToDocument(File file) throws JDOMException {
try {
document = new SAXBuilder().build(readFile(file));
return document;
} catch (JDOMException e) {
throw e;
} catch (IOException e) {
throw new JDOMException("Can't find ["+file.getPath()+"]file !");
}
}
@SuppressWarnings("unchecked")
public static List
throws JDOMException {
return XPath.newInstance(express).selectNodes(fileToElement(file));
}
public static Element fileToElement(File file) throws JDOMException{
return fileToDocument(file).getRootElement();
}
@SuppressWarnings("unchecked")
public static List
throws JDOMException {
return fileToElement(file).getChildren();
} public static List
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯