您的位置:首页 > 编程语言 > Java开发

【java】jdom解析xml文件

2012-12-05 16:13 253 查看
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文件可以当做一个小型数据库?呵呵。。。本人觉得应该可以。他可以对其内容,增删改查功能,所以应该可以当做一个小型数据库。

======本人才疏学浅,如有讲解错误。望海涵。呵呵===========
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: