阅读背景:

利用jdom解析xml配置文件,并且按照xml格式输出,进行缩进

来源:互联网 
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 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: