Gửi Thông Điệp Yêu Thương vanbinh_bt nhắn với all: thi sao rồi mấy đồ...[Mr]-Boom nhắn với All K4: Chúc mọi người có 1 kỳ thi thật tốt !!lặng lẽ yêu nhắn với nguyễn ngọc huy: chúc mi có một kỳ thi tốt nha. vẫn nhớ mingocthongcctm05f nhắn với QNGAI: hãy sát cánh bên nhau.....hi.......trinhvh91 nhắn với TM03B: TM03B đoàn kết đi chơi cuối khóa nghenNgười vô cảm nhắn với nguyenlu: Ai vậy? T vẫn bước đi trong yêu thương mà ^^381 nhắn với neyugn: mong rằng chúng ta sẽ đi đến cái đích cuối cùng !nguyenlu nhắn với Người vô cảm: Hãy bước đi trong sự yêu thươnglenguyen_alone nhắn với Young Bin: tại sao nhok lại iu bin chứ nhỉ???hp thành công nhá!!binhcaolinh nhắn với Đen Trần: Cố gắng học chăm chỉ nhé em! Gửi Thông Điệp Yêu Thương


BẢNG GIÁ QUẢNG CÁO TẠI DIỄN ĐÀN 2MIT.ORG 2012

Hiện kết quả từ 1 tới 1 của 1

Chủ đề: Một ví dụ đơn giản về Java DOM XML

  1. #1
    Tham gia ngày
    Sep 2009
    Bài gửi
    217
    Cảm ơn
    294
    Được cảm ơn 192/83 bài viết
    Blog Entries
    7
    3
    Advanced
     
     
    Số lần cộng|trừ: 0 lần

    Default Một ví dụ đơn giản về Java DOM XML

    (Sưu tầm)

    Code:
    import java.io.*;
    
    import org.w3c.dom.*;
    
    import javax.xml.parsers.*;
    
    import javax.xml.transform.*;
    import javax.xml.transform.dom.*;
    import javax.xml.transform.stream.*;
    
    public class DomXmlExample {
    
        /**
         * Our goal is to create a DOM XML tree and then print the XML.
         */
        public static void main (String args[]) {
            new DomXmlExample();
        }
    
        public DomXmlExample() {
            try {
                /////////////////////////////
                //Creating an empty XML Document
    
                //We need a Document
                DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
                Document doc = docBuilder.newDocument();
    
                ////////////////////////
                //Creating the XML tree
    
                //create the root element and add it to the document
                Element root = doc.createElement("root");
                doc.appendChild(root);
    
                //create a comment and put it in the root element
                Comment comment = doc.createComment("Just a thought");
                root.appendChild(comment);
    
                //create child element, add an attribute, and add to root
                Element child = doc.createElement("child");
                child.setAttribute("name", "value");
                root.appendChild(child);
    
                //add a text element to the child
                Text text = doc.createTextNode("Filler, ... I could have had a foo!");
                child.appendChild(text);
    
                /////////////////
                //Output the XML
    
                //set up a transformer
                TransformerFactory transfac = TransformerFactory.newInstance();
                Transformer trans = transfac.newTransformer();
                trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
                trans.setOutputProperty(OutputKeys.INDENT, "yes");
    
                //create string from xml tree
                StringWriter sw = new StringWriter();
                StreamResult result = new StreamResult(sw);
                DOMSource source = new DOMSource(doc);
                trans.transform(source, result);
                String xmlString = sw.toString();
    
                //print xml
                System.out.println("Here's the xml:\n\n" + xmlString);
    
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }
    Bài viết mang tính tham khảo.

    --------------------------------------------------
    Xem các chủ đề cùng chuyên mục:



    010000000000110000000010001000110011001000
    000111111110001001100011000000001111111100
    100001111001000011111111100010001110000000
    010001111001000111001100111000001111110000
    100001111000001110000100011100001110000000
    100111111110011110010000011110001111111100
    001000000000000000100000010100010000000010
    011100101101100011101001101010110100111010


  2. Có tổng cộng 2 thành viên cảm ơn IME cho bài viết này

    integer (11-01-2011), viethung_9x  (29-01-2011)

Thông tin về chủ đề này

Users Browsing this Thread

Hiện giờ đang có 1 người xem chủ đề này. (0 thành viên 1 khách)

     

Chủ đề giống nhau

  1. Trả lời: 6
    Bài mới gửi: 20-01-2011, 02:03 PM
  2. BÀI 1 : Tìm hiểu cấu trúc và cú pháp của XML
    By viethung_9x in forum Ngôn ngữ lập trình web
    Trả lời: 4
    Bài mới gửi: 12-01-2011, 12:54 PM
  3. Nhập môn Java
    By congthangitvn in forum Lập trình máy tính
    Trả lời: 0
    Bài mới gửi: 15-05-2010, 08:18 AM
  4. Sách học Java của Aptech
    By jinyotino in forum Lập trình máy tính
    Trả lời: 3
    Bài mới gửi: 28-08-2009, 02:25 AM
  5. Giáo trình XML
    By jinyotino in forum Lập trình máy tính
    Trả lời: 0
    Bài mới gửi: 26-08-2009, 08:54 PM

751
Lượt xem

Share