用JDOM讀取XML示例

          /*
           * Created on 2004-8-21
           *
           * TODO To change the template for this generated file go to
           * Window - Preferences - Java - Code Style - Code Templates
           
          */


          import org.jdom.Document;
          import org.jdom.Element;
          import org.jdom.input.SAXBuilder;
          import org.jdom.output.Format;
          import org.jdom.output.XMLOutputter;

          /**
           * 
          @author kingwong
           *
           * TODO To change the template for this generated type comment go to
           * Window - Preferences - Java - Code Style - Code Templates
           
          */

          public class myJDOM {
           
          public static void main(String[] args){
           SAXBuilder sb 
          = new SAXBuilder();
           
          try
           

           Document doc 
          = sb.build("C:/ResultSet.xml");
           Element root 
          = doc.getRootElement();
           
           String str1 
          = root.getAttributeValue("type");
           System.out.println(
          "Root Element's comment attribute is : " + str1);
           String str2 
          = root.getChild("DataRow").getAttributeValue("size");
           System.out.println(
          "sex Element's value attribute is : " + str2);
           String str3 
          = root.getChildText("ShowRow");
           System.out.println(
          "name Element's content is :" + str3);
           String str4 
          = root.getChild("DataRow").getChildText("city_name");
           System.out.println(
          "contact Element's telephone subelement content is : " + str4 + "\n");
           Element inputElement 
          = root.getChild("Contact"); 
           inputElement.addContent(
          new Element("email").setAttribute("value","wanghua@cyberobject.com"));
           XMLOutputter xmlOut 
          = new XMLOutputter(Format.getPrettyFormat());
           String outStr 
          = xmlOut.outputString(root);
           System.out.println(outStr);
           }

           
          catch(Exception e)
           
          {
           e.printStackTrace();
           }

          }

          }

          另一個例子:
          import org.jdom.*;
          import org.jdom.input.*;
          //import org.jdom.output.*;
          import java.io.*;
          import java.util.*;

          public class Cute {

           
          public static void main(String args[]) {
            
          try {
             
          /*
              * 用無變元構(gòu)造函數(shù)構(gòu)造一個SAXBuilder對象, 用sax解析器從文件中構(gòu)造文檔,
              * SAXBuilder偵聽sax事件并從內(nèi)存中建立一個相應(yīng)的文檔
              
          */
             SAXBuilder sb 
          = new SAXBuilder();
             
          // 創(chuàng)建文檔
             Document doc = sb.build(new FileInputStream("c:/ResultSet.xml"));
             
          // 加入一條處理指令
             ProcessingInstruction pi = new ProcessingInstruction(
               
          "xml-stylesheet",
               
          "href=\"bookList.html.xsl\" type=\"text/xsl\"");
             
          // 把這條處理指令,加入文檔中
             doc.addContent(pi);
             
          // 獲得這個文檔的根元素
             Element el = doc.getRootElement();
             
             printElement(el);
          /*   
             // 得到第一個子元素的子元素,卻完全忽略其內(nèi)容
             Element book = (Element) ls.get(0);
             // 給這個子元素添加一條屬性,
             Attribute attr = new Attribute("hot", "true");
             book.setAttribute(attr);
             // 獲得這個元素的子元素(指定)以及其值
             Element el2 = book.getChild("author");
             // 輸出這個元素的值
             System.out.println(el2.getName());
             // 給這個元素的值改個名字
             el2.setText("cute");
             // 再獲得這個元素的子元素(指定)
             Element el3 = book.getChild("price");
             // 給這個值換個值
             el3.setText(Float.toString(50.0f));
             XMLOutputter xml = new XMLOutputter();
             xml.output(doc, new FileOutputStream("cute.xml"));
          */
            } 
          catch (Exception e) {
             System.out.println(e.getMessage());

            }

           }
               
          private static void printElement(Element el)
               {
                   List ls 
          = el.getContent();
                   Iterator i 
          = ls.iterator();
                   
          while (i.hasNext()) {
                       Object o 
          = i.next();
                       
          if (o instanceof Text)/*使用instanceof 來獲得所需要的內(nèi)容*/
                       {
                           Text t
          =(Text)o;
                           System.out.println(
          "Text: " + t.getText());}
                       
          else if(o instanceof Attribute)
                           System.out.println(
          "Attribute: " + o);
                       
          else if (o instanceof Element)
                       {
                           System.out.println(
          "Element: " + ((Element) o).getName());
                           Element oe 
          = (Element)o;
                           System.out.println(
          "Value: "+oe.getValue());
                       }
                   }
               }
          }

          posted on 2007-08-24 16:10 靈魂守護(hù)者 閱讀(545) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導(dǎo)航

          統(tǒng)計

          留言簿(1)

          文章分類(2)

          文章檔案(4)

          收藏夾(9)

          搜索

          最新評論

          主站蜘蛛池模板: 武平县| 台中县| 闸北区| 偃师市| 前郭尔| 绥化市| 姚安县| 晋城| 呼伦贝尔市| 永定县| 濮阳市| 麟游县| 嘉义市| 同心县| 博湖县| 昭觉县| 仙桃市| 辉县市| 许昌县| 江都市| 朝阳区| 琼中| 丰都县| 天长市| 彰化县| 枝江市| 锡林浩特市| 女性| 电白县| 长春市| 宣恩县| 温宿县| 海林市| 安阳县| 聂拉木县| 河曲县| 衡水市| 陈巴尔虎旗| 九龙坡区| 乌兰县| 两当县|