DOM4J解析xml文档

DOM4J是一个简单,灵活的开放源代码的库,DOM4J是由早期开发JDOM的人分离出来而后独立开发的,与JDOM不同的是,Dom4j使用接口和抽象基类,虽然Dom4j的API相对要复杂一些,但它提供了比JDOM更好的灵活性。

Hibernate,包括sun公司的JAXM也用了Dom4j,使用Dom4j开发,需要下载Dom4j相应的jar文件。

DOM4J解析xml文档_第1张图片

在docs文件夹下,查看帮助文档。

DOM4J解析xml文档_第2张图片

实例:
以下是需要操作的xml文件:(src/book.xml)

<书架> 
  <书 name="yyyy"> 
    <售价>20元  
    <售价>30元
    <书名>西游记  
    <作者>吴承恩  
    <售价>119元 
   
  <书 name="xxx"> 
    <书名>红楼梦  
    <作者>曹雪芹  
    <售价>119元 
   

============================
//读取xml文档
	public void read() throws Exception{
		SAXReader reader  = new SAXReader();   
		Document document = reader.read(new File("src/book.xml"));
		Element root = document.getRootElement();
		Element bookname = root.element("书").element("书名");
		System.out.println(bookname.getText());
	}
//读取属性值
	public void readAttr() throws Exception{
		SAXReader reader  = new SAXReader();
		Document document = reader.read(new File("src/book.xml"));
		Element root = document.getRootElement();  //获取根节点
		String value = root.element("书").attributeValue("name");
		System.out.println(value);
	}
//向xml文档中添加数据标签
	public void add() throws Exception{
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/book.xml"));
		//创建标签
		Element price = DocumentHelper.createElement("售价");
		price.setText("120元");
		//添加节点
		document.getRootElement().element("书").add(price);
		//将数据写入到xml文件 为了防止乱码
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		
		XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"), format);
		writer.write(document);
		writer.close();
	}
	//修改节点
	public void update() throws Exception{
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/book.xml"));
		Element price = (Element) document.getRootElement().element("书").elements("售价").get(2);
		price.setText("119元");
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"), format);
		writer.write(document);
		writer.close();
	}
	//删除节点  <售价>109  
	public void delete() throws Exception{
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/book.xml"));
		//删除
		Element price = (Element) document.getRootElement().element("书").elements("售价").get(1);
		price.getParent().remove(price);
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"), format);
		writer.write(document);
		writer.close();
	}
	//向指定位置增加节点
	public void add2() throws Exception{
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/book.xml"));
		Element price = DocumentHelper.createElement("售价");
		price.setText("30元");
		List list = document.getRootElement().element("书").elements();
		list.add(1, price);
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"), format);
		writer.write(document);
		writer.close();
	}
	//利用xparse 需要掌握一定的xparse语法
	public void findWithXpath() throws Exception{
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/book.xml"));
		Element e = (Element) document.selectNodes("//书名").get(1);
		System.out.println(e.getText());
	}
	public void findUser() throws Exception{
		String username="aaa";
		String password="1233";
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/user.xml"));
		//注意单引号
		Element e = (Element) document.selectSingleNode("//user[@username='"+username+"' and @password='"+password+"']");//断字符串,先双引号,再双加号
		if(e!=null){
			System.out.println("登录成功");
		}else{
			System.out.println("用户名和密码不正确");	
		}
	}
}



你可能感兴趣的:(javaweb学习之路)