用原生的dom来解析xml(就是传统的DocumentBuilderFactory---->DocumentBuilder--->Document的方式)确实比较费力;好在现在有很多第三方的解析工具了,在这方面jdom就相当优秀,我个人也比较喜欢用;其实说到xml文件,在开发中对其的操作无非就是增删改查了,其中最常用的就是查了,不过为了全面,在这里把所有的总结了;
代码和所用的jar放在附件中,jdom.xml文件的路径是 工程名字\src\jdom.xml
package jdom;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class JdomParse {
private static String fileName="jdom.xml";
private static SAXBuilder saxBuilder;
private static Document document;
private static final String classpath=Thread.currentThread().getContextClassLoader().getResource("").getPath();
//private static final String classpath=JdomParse.class.getClassLoader().getResource("").getPath();
private static String filePath;
private static File workFile;
static{
saxBuilder=new SAXBuilder();
try {
filePath=classpath+fileName;
workFile=new File(filePath);
document=saxBuilder.build(new File(filePath));
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**遍历xml文件中的元素*/
public void parse(){
Element root=document.getRootElement();
System.out.println("root---"+root.getName());
Element title=root.getChild("title"); //根据元素名字获得元素
System.out.println("title----"+title.getText());
Element contents=root.getChild("contents");
List chapterList=contents.getChildren("chapter"); //根据和元素名对应的孩子序列
System.out.println("list--size---"+chapterList.size());
for (int i = 0; i < chapterList.size(); i++) {
Element chapter=(Element) chapterList.get(i);
List topicList=chapter.getChildren("topic");
for (int j = 0; j < topicList.size(); j++) {
Element topic=(Element) topicList.get(j);
String name=topic.getChild("name").getText();
String address=topic.getChildText("address");
System.out.println("name---"+name);
System.out.println("address---"+address);
String email=topic.getChildText("email");
if(email!=null&&!email.equalsIgnoreCase("")){
System.out.println("emai---"+email);
}
}
}
}
/**向xml文件中增加一个元素*/
public void insert(){
Element root=document.getRootElement();
Element appendix=new Element("appendix");
appendix.setAttribute("id", "101");
appendix.setText("after Title");
Element author=new Element("author");
author.setText("qinjk");
appendix.addContent(author);
root.addContent(appendix);
try {
XMLOutputter xmlOutputter=new XMLOutputter();
OutputStream os=new FileOutputStream(workFile);
xmlOutputter.output(document, os);
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**从xml文件中删除一个元素*/
public void remove(){
Element root=document.getRootElement();
List appendixList=root.getChildren("appendix");
for (int i = 0; i <appendixList.size(); i++) {
Element appendix=(Element) appendixList.get(i);
if(appendix.getAttributeValue("id").equalsIgnoreCase("101")){
try {
root.removeContent(appendix);
XMLOutputter xmlOutputter=new XMLOutputter();
OutputStream os=new FileOutputStream(workFile);
xmlOutputter.output(document, os);
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**更新xml文件中某一元素的值*/
public void modify(){
Element root=document.getRootElement();
List appendixList=root.getChildren("appendix");
for (int i = 0; i <appendixList.size(); i++) {
Element appendix=(Element) appendixList.get(i);
if(appendix.getAttributeValue("id").equalsIgnoreCase("101")){
try {
appendix.setAttribute("id","22222");
XMLOutputter xmlOutputter=new XMLOutputter();
OutputStream os = new FileOutputStream(workFile);
xmlOutputter.output(document, os);
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new JdomParse().parse();
}
}