当前位置:编程学习 > XML/UML >>

[java]jdom解析xml文件

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.zzzyk.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();//获得根节点<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>
 

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,