package 解析xml文件;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class 自己解析xml文件 {
public static void main(String[] args) throws JDOMException, IOException {
SAXBuilder sax = new SAXBuilder();
Document doc = sax.build(new File("f:/web.xml"));
Element root = doc.getRootElement();
解析(root,0);
}
private static void 解析(Element node, int depth) {
List list = node.getChildren();
//System.out.println(list.size());
System.out.print(repeatString(" ", depth) + "<" + node.getName() + ">");
if(node.getText().trim().equals("")){
System.out.println();
}else{
System.out.print(node.getText().trim());
}
for(int i = 0 ; i<list.size() ;i++){
Element e = (Element) list.get(i);
解析(e,depth + 1);
}
if(node.getText().trim().equals("")){
System.out.print(repeatString(" ", depth));
}
System.out.println("</" + node.getName() + ">");
}
private static String repeatString(String s, int depth) {
StringBuffer sb = new StringBuffer();
for(int i = 0 ; i < depth; i++){
sb.append(s);
}
return sb.toString();
}
}
package 解析xml文件;
import java.io.File;
import