zoukankan      html  css  js  c++  java
  • Java之Dom4J(Read )

    package anby.com;
    
    import java.util.Iterator;
    
    public class Dom4JRead {
    
    	public static void main(String[] args) throws DocumentException {
    	 //Dom4JReader();
    	 
    	 SAXReader saxreader=new SAXReader();
    	 Document doc=saxreader.read("Book.xml");
    	 Node node =doc.selectSingleNode("//book[@id='boo1']");
    	Element  ele=(Element)node;
    	String titile=ele.elementText("titile");
    	 System.out.println(titile);//呐喊
        }
    
    	public static void Dom4JReader() throws DocumentException {
    		SAXReader reader=new SAXReader();
    		Document document=reader.read("Book.xml")	;//获取dom到内存
    	   Element root=	document.getRootElement();//获取根元素
    	   Iterator i=root.elementIterator(); //使用迭代器
    		while (i.hasNext())
    			{
    			  Element ele=(Element)i.next();    //迭代取出Element
    			  Attribute attribute=  ele.attribute("id");//获取id属性
    			  System.out.println( attribute.getValue());
    			  //取出ele中的元素
    			   Iterator it=ele.elementIterator();
    			   while(it.hasNext())
    			   {
    				   Element childEle=(Element)it.next();//迭代元素
    				  System.out.println( childEle.getText());//获取ChildEle中文本内容
    			   }
    			}
    	}
    }
    //result:
    //boo1
    //呐喊
    //18
    //boo2
    //彷徨
    //15
    

      

  • 相关阅读:
    java基础 ——String类型
    WinSCP密钥登录,实现文件上传
    linux 文件压缩,以及解压
    win10的资源管理器,边框不见了
    在360浏览器中打开表单,回显错误数据
    java返回数据工具类
    WEB工具类
    JSON 的几种转换
    二维码生成工具类
    XML的几种转换
  • 原文地址:https://www.cnblogs.com/anbylau2130/p/3019300.html
Copyright © 2011-2022 走看看