JAVA jdom解析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 JdomTest {

	public static void main(String[] args) throws JDOMException, IOException {
		// TODO Auto-generated method stub
		//获取解析xml文件路径
		String path = System.getProperty("user.dir")+"/bin/stdent.xml";//System.getProperty("user.dir")得到的是项目真实路径
		File file = new File(path);
		//通过文件获取xml全部内容
		SAXBuilder saxBuilder = new SAXBuilder();
		Document document = saxBuilder.build(file);
		//获取xml文件根节点
		Element root = document.getRootElement();
		//获取根节点的属性
		String name = root.getAttributeValue("name");
		//获得子节点
		Element child = root.getChild("colgroup");
		//获得全部子节点
		List<Element> cols = child.getChildren();
		
		
		
	}

}

你可能感兴趣的:(java,xml)