<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    用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 {
       
    /*
        * 用無變元構造函數構造一個SAXBuilder對象, 用sax解析器從文件中構造文檔,
        * SAXBuilder偵聽sax事件并從內存中建立一個相應的文檔
        
    */
       SAXBuilder sb 
    = new SAXBuilder();
       
    // 創建文檔
       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);
    /*   
       // 得到第一個子元素的子元素,卻完全忽略其內容
       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 來獲得所需要的內容*/
                 {
                     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 靈魂守護者 閱讀(540) 評論(0)  編輯  收藏


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


    網站導航:
     
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    統計

    留言簿(1)

    文章分類(2)

    文章檔案(4)

    收藏夾(9)

    搜索

    最新評論

    主站蜘蛛池模板: 2022久久国产精品免费热麻豆| a免费毛片在线播放| 日本免费大黄在线观看| 伊人久久综在合线亚洲91| 美女的胸又黄又www网站免费| 午夜免费福利在线| 国产v亚洲v天堂a无| 在人线av无码免费高潮喷水| 亚洲人成网站看在线播放| 全免费毛片在线播放| 亚洲精品123区在线观看| 免费一本色道久久一区| 亚洲精品无码一区二区| 精品无码国产污污污免费| 国产精品亚洲专区无码唯爱网| 免费日韩在线视频| 国产黄在线播放免费观看| 久久精品国产亚洲综合色| 日本xxxx色视频在线观看免费| 亚洲另类小说图片| 免费黄色小视频网站| 污视频网站免费观看| 国精无码欧精品亚洲一区| 老汉精品免费AV在线播放| 中中文字幕亚洲无线码| 国产成人免费片在线观看 | 亚洲自偷自偷偷色无码中文| 国产免费区在线观看十分钟| 亚洲av福利无码无一区二区| 亚洲高清中文字幕免费| 亚洲国产精品99久久久久久| 久久久久一级精品亚洲国产成人综合AV区| 两性色午夜视频免费网| 亚洲精品国产啊女成拍色拍| 国产精品四虎在线观看免费| 亚洲黄片手机免费观看| 亚洲成年人电影在线观看| 免费在线观看黄色毛片| 免费女人高潮流视频在线观看 | 亚洲国产精品久久久久网站| 97在线观免费视频观看|