[java]jdom解析xml文件(一)

2014-11-24 10:41:01 · 作者: · 浏览: 0

java中有四种分别解析xml文件。分别是,DOM,SAX,DOM4J,JDOM四种。我第一篇就介绍用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") www.2cto.com
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();//获得根节点
List list = root.getChildren();//获得根节点的子节点
for(Element e:list) {
System.out.println("出版社:"+e.getAttributeva lue("genre"));
System.out.println("防伪码:"+e.getAttributeva lue("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();//获得根节点

Element element=new Element("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();//获得根节点
List list=root.getChildren();//获得所有根节点的子节点
for(Element e:list){
//删除genre="上海出版社"这个book节点
if(str.equals(e.getAttributeva lue("genre"))){
root.removeContent(e);
System.out.println("删除成功!!!");
break;
}
}
//文件处理
XMLOutputter out = new XMLOutputter();
out.output(document, new FileOutputStream("src/bookstore.xml"));
}
//修改xml文件
@Supp