------------------------
package ParseXML; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.List; import org.jdom.Attribute; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; /** * @author 小王同学 * * */ public class TestJDOM { public static void main(String[] args) throws FileNotFoundException, JDOMException, IOException { //1.创建一个SABuilder对象 SAXBuilder sb =new SAXBuilder(); //2.调用build方法,得到Document对象(通过IO流) Document doc = sb.build(new FileInputStream("book.xml")); //3.获取跟节点 Element root= doc.getRootElement();//获取根节点元素 //4.获取跟节点节点的直接子节点的集合 List<Element> bookEle =root.getChildren();//获取所有的子节点 //5.遍历集合,得到book的每一个子节点 for(int i=0;i<bookEle.size();i++){ Element book= bookEle.get(i); List <Element>rrElelist = bookEle.get(i).getChildren(); List<Attribute> attlist= book.getAttributes(); for(Attribute attr:attlist){ System.out.println(attr.getName()+" "+attr.getValue()); } for(Element e :rrElelist){ System.out.println("节点名称:"+e.getName()+" 节点内容:"+e.getValue()); } } } }
-----------------这里是输入结果.