【java】jdom解析xml文件

java中有四种分别解析xml文件。分别是,DOMSAXDOM4JJDOM四种。我第一篇就介绍用Jdom解析XML。本人觉得这四种学习其中一种即可。其余三中解析思想差不了多少。况且这四种介绍优缺点可在网上查询,本人就不多说了。一下就是我写的一个例子,例子比较仔细估计都能看得懂。

测试java:

package com.rthb.test;

import java.io.FileOutputStream;
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;
import org.jdom.output.XMLOutputter;

public class TestXml {

	/**
	 * 创建人:zhanglx
	 * 创建时间:上午11:28:19
	 * 描述   :读取xml文件
	 * @throws IOException 
	 * @throws JDOMException 
	 * @throws IOException 
	 * @throws JDOMException 
	 */
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws JDOMException, IOException {
		//ReadXml();//读取xml文件
		//AddXml();//添加xml信息
		//DeleteXml("上海出版社");//删除genre="上海出版社"这个book节点
		UpdateXml("人民出版社");//修改genre="人民出版社"这个book节点
	}
	//读取xml文件
	@SuppressWarnings("unchecked")
	public static void ReadXml()throws JDOMException, IOException {
		 SAXBuilder builder = new SAXBuilder();//实例JDOM解析器
		 Document document = builder.build("src/bookstore.xml");//读取xml文件
		 Element root = document.getRootElement();//获得根节点<bookstore>
		 List<Element> list = root.getChildren();//获得根节点的子节点
		 for(Element e:list) {
		   System.out.println("出版社:"+e.getAttributeValue("genre"));
		   System.out.println("防伪码:"+e.getAttributeValue("ISBN"));
		   System.out.println("书名:"+e.getChildText("title"));
		   System.out.println("作者:"+e.getChildText("author"));
		   System.out.println("价格:"+e.getChildText("price"));
		   System.out.println("==========================================");
		 }
	}
	//添加xml文件信息(即向xml中添加一个节点信息)
	@SuppressWarnings("unchecked")
	public static void AddXml() throws JDOMException, IOException{
		SAXBuilder builder = new SAXBuilder();//实例JDOM解析器
		Document document = builder.build("src/bookstore.xml");//读取xml文件
		Element root = document.getRootElement();//获得根节点<bookstore>
		
		Element element=new Element("book");//添加一个新节点<book></book>
		element.setAttribute("genre","上海出版社");//给book节点添加genre属性
		element.setAttribute("ISBN","6-3631-4");//给book节点添加ISBN属性
		Element element1=new Element("title");
		element1.setText("悲伤逆流成河");
		Element element2=new Element("author");
		element2.setText("郭敬明");
		Element element3=new Element("price");
		element3.setText("32.00");
		element.addContent(element1);
		element.addContent(element2);
		element.addContent(element3);
		root.addContent(element);
		document.setRootElement(root);
		//文件处理
		XMLOutputter out = new XMLOutputter();
		out.output(document, new FileOutputStream("src/bookstore.xml"));
	}
	//删除xml信息(即删除一个xml的节点)
	@SuppressWarnings("unchecked")
	public static void DeleteXml(String str)throws JDOMException, IOException{
		SAXBuilder builder = new SAXBuilder();//实例JDOM解析器
		Document document = builder.build("src/bookstore.xml");//读取xml文件
		Element root = document.getRootElement();//获得根节点<bookstore>
		List<Element> list=root.getChildren();//获得所有根节点<bookstore>的子节点<book>
		for(Element e:list){
			//删除genre="上海出版社"这个book节点
			if(str.equals(e.getAttributeValue("genre"))){
				root.removeContent(e);
				System.out.println("删除成功!!!");
				break;
			}
		}
		 //文件处理
		  XMLOutputter out = new XMLOutputter();
		  out.output(document, new FileOutputStream("src/bookstore.xml"));
	}
	//修改xml文件
	@SuppressWarnings("unchecked")
	public static void UpdateXml(String str)throws JDOMException, IOException{
		SAXBuilder builder = new SAXBuilder();//实例JDOM解析器
		Document document = builder.build("src/bookstore.xml");//读取xml文件
		Element root = document.getRootElement();//获得根节点<bookstore>
		List<Element> list=root.getChildren();//获得所有根节点<bookstore>的子节点<book>
		for(Element e:list){
			//删除genre="上海出版社"这个book节点
			if(str.equals(e.getAttributeValue("genre"))){
				e.getChild("title").setText("111111111");
				System.out.println("修改成功!!!");
				break;
			}
		}
		 //文件处理
		  XMLOutputter out = new XMLOutputter();
		  out.output(document, new FileOutputStream("src/bookstore.xml"));
	}
}
 

测试xml:bookstore.xml

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book genre="江南出版社" ISBN="1-3631-4">
    <title>盗墓笔记</title>
    <author>南派三叔</author>
    <price>25.00</price>
</book>
<book genre="延边出版社" ISBN="2-3631-4">
    <title>三重门</title>
    <author>韩寒</author>
    <price>35.00</price>
</book>
<book genre="华夏出版社" ISBN="3-3631-4">
    <title>平凡的世界</title>
    <author>路遥</author>
    <price>27.00</price>
</book>
<book genre="湖北出版社" ISBN="4-3631-4">
    <title>随遇而安</title>
    <author>孟非</author>
    <price>30.00</price>
</book>
<book genre="人民出版社" ISBN="5-3631-4">
    <title>111111111</title>
    <author>余秋雨</author>
    <price>40.00</price>
</book>
</bookstore>

使用:jdom-1.0.jar包

总结:在读取xml文件时候。大家是不是觉得有点像用io流读取一个文件?其实读取xml文件,本来就是文件读取的。只不过此时的文件是xml属于。有问有xml文件内部有一定的特性,所以需要一层一层的往下读取。如读取根节点,然后循环根节点内部的子节点,最后在循环读取每一个子节点内部个属性、元素的值。

在添加xml文件时也是如此。首先读取文件,找到根节点,然后要添加沈阳的子节点、在子节点上要添加什么样的属性、元素。
其实做了以上的操作,大家不是不觉得xml文件可以当做一个小型数据库?呵呵。。。本人觉得应该可以。他可以对其内容,增删改查功能,所以应该可以当做一个小型数据库。

======本人才疏学浅,如有讲解错误。望海涵。呵呵===========

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值