Chinaunix首页 | 论坛 | 博客
  • 博客访问: 522100
  • 博文数量: 147
  • 博客积分: 10105
  • 博客等级: 上将
  • 技术积分: 1594
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-14 10:18
文章分类

全部博文(147)

文章存档

2011年(4)

2010年(4)

2009年(6)

2008年(5)

2007年(40)

2006年(88)

我的朋友

分类: Java

2006-07-17 17:11:24

import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
import java.util.*;
import java.io.File;

public class TestSax
{
 public static void main(String dd[]){
  try{
  SAXBuilder sh = new SAXBuilder();
  Document dc = sh.build(new File("fibo.xml"));
  Element el = dc.getRootElement();
  String name = el.getName();
  System.out.println(name);
  List elch = el.getChildren();
  System.out.println(elch.toString());
  Iterator it = elch.iterator();
  while(it.hasNext()){
  Element e = (Element)it.next();
  String b = e.getAttributeValue("ProdTypeId");
  System.out.println(b);
   List temp = e.getChildren("ab");
          Element te = (Element) temp.get(0);
         String  result = te.getTextNormalize();
   System.out.println("-----"+result);
  }
  }catch(Exception e){
   e.printStackTrace();
  }
 }
}
 
 
先招呼段程序.別看這段沒什么,不過是SAX解析XML的一個小程序..因為要讀取xml.所以就從這個地方入手.網上擻索暸些資料.把xml的文件也放上來..呵呵
 


 
  aaa
 
  

 
哈哈..順便把註釋我給加上..
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
import java.util.*;
import java.io.File;

public class TestSax
{
 public static void main(String dd[]){
  try{
  SAXBuilder sh = new SAXBuilder();                                 //construct Builder
  Document dc = sh.build(new File("fibo.xml"));               //load
  Element el = dc.getRootElement();                                //root
  String name = el.getName();                                          //root's name
  System.out.println(name);                                             //print name
  List elch = el.getChildren();                                            //get sub node
  System.out.println(elch.toString());                               //print
  Iterator it = elch.iterator();                                            //iterator
  while(it.hasNext()){                                                        //if it have next is true continute
  Element e = (Element)it.next();                                      //load object
  String b = e.getAttributeValue("ProdTypeId");              //get attribute
  System.out.println(b);                                                    //print b
   List temp = e.getChildren("ab");                                    //get subnode
          Element te = (Element) temp.get(0);                      //load first object
         String  result = te.getTextNormalize();                    //read message
   System.out.println("-----"+result);                                 //print
  }
  }catch(Exception e){
   e.printStackTrace();
  }
 }
}
 
最后的運行結果是:
---------- java ----------
BusiConfig
[[Element: ]]
00002
-----aaa
输出完成 (耗时 0 秒) - 正常终止
 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=912175

阅读(667) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:hibernate+struts+mysql+eclipse Project

给主人留下些什么吧!~~