J2EE學習之路---JavaBean和DOM

來源:互聯網
上載者:User

標籤:

什麼是JavaBean

JavaBean 是一種JAVA語言寫成的可重用組件。說白了,就是一個類,成員都是私人的,每個成員都有其各自的getXxx方法和setXxx方法。

什麼是DOM

文件物件模型(Document Object Model,簡稱DOM),是W3C組織推薦的處理可擴充標誌語言的標準編程介面。說白了,就是W3C組織推薦的一種可以解析XML的方式。我們寫的程式要解析XML,首先調用解析開發包(jaxp,dom4j),解析開發包裡就調用瞭解析器(Crimson,Xerces),解析器就和DOM打交道。

寫個JavaBean瞅瞅?

代碼如下:

package cn.itcase.domain;public class Book {        private String id;    private String title;    private String price;            public Book(String id, String title, String price) {        super();        this.id = id;        this.title = title;        this.price = price;    }    public Book() {        super();    }        @Override    public String toString() {        return "Book [id=" + id + ", title=" + title + ", price=" + price + "]";    }        public String getId() {        return id;    }        public void setId(String id) {        this.id = id;    }        public String getTitle() {        return title;    }        public void setTitle(String title) {        this.title = title;    }        public String getPrice() {        return price;    }        public void setPrice(String price) {        this.price = price;    }        public static void main(String[] args) {        Book book = new Book();                book.setId("b001");        book.setTitle("Java Core");        book.setPrice("98");                System.out.println(book);                Book book2 = new Book("b002", "C++", "99");                System.out.println(book2);    }    }

值得一提的是,在myeclipse中,可以使用快速鍵(Alt + Shift +s)調用快速設定get和set方法,建構函式,重寫toString()方法等。

 

DOM尼瑪怎麼用?

再上個代碼瞅瞅:

    /*     * 拋出所有的異常     */    private static void builder() throws Exception {        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();                DocumentBuilder builder = factory.newDocumentBuilder();                System.out.println(builder);            }

這裡用到了 DocumentBuilderFactory 這個抽象類別,大家知道,抽象類別是不能去new的,我們通過尋找JDK的文檔,發現DocumentBuilderFactory 裡有一個靜態,獲得此類執行個體的一個方法,叫做newInstance(),我們調用這個方法,就獲得的這個執行個體,同樣,DocumentBuilder,即我們說的xml解析器也是抽象類別,不能new,尋找文檔,然後用此執行個體中的newDocumentBuilder(),即可獲得一個xml檔案的解析器了。

 

今天的學習就到這裡,明天即要利用解析器和解析開發包,讀取xml中的檔案內容並擷取了。

J2EE學習之路---JavaBean和DOM

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.