中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

JDOM生成、解析XML實例代碼

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
    import java.io.File;  
    import java.io.FileNotFoundException;  
    import java.io.FileOutputStream;  
    import java.io.IOException;  
    import java.util.List;  
       
    import org.jdom.Attribute;  
    import org.jdom.Comment;  
    import org.jdom.Document;  
    import org.jdom.Element;  
    import org.jdom.JDOMException;  
    import org.jdom.input.SAXBuilder;  
    import org.jdom.output.Format;  
    import org.jdom.output.XMLOutputter;  
       
    /** 
     * 
     * jdom生成與解析XML文檔 
     * 
     */  
    public class JdomDemo{  
       
        Document document = new Document();  
       
        /** 
         * 利用JDom進行xml文檔的寫入操作 
         */  
        public void createXml(File file) {  
       
            // 1.創(chuàng)建元素 及 設(shè)置為根元素  
            Element employees = newElement("employees");  
            document.setContent(employees);  
       
            // 2.創(chuàng)建注釋 及 設(shè)置到根元素上  
            Comment commet = new Comment("thisis my comment");  
            employees.addContent(commet);  
       
            // 3.創(chuàng)建元素  
            Element element1 = newElement("employee");  
       
            // 3.1 設(shè)置元素的屬性名及屬性值  
            element1.setAttribute(newAttribute("id", "0001"));  
       
            // 3.2 創(chuàng)建元素的屬性名及屬性值  
            Attribute nameAttr = newAttribute("name", "wanglp");  
       
            // 3.3 設(shè)置元素名及文本  
            Element sexEle = newElement("sex");  
            sexEle.setText("m");  
            // 設(shè)置到上層元素上  
            element1.addContent(sexEle);  
       
            // 設(shè)置元素  
            Element ageEle = newElement("age");  
            ageEle.setText("22");  
            element1.addContent(ageEle);  
       
            // 設(shè)置為根元素的子元素  
            employees.addContent(element1);  
            // 將元素屬性設(shè)置到元素上  
            element1.setAttribute(nameAttr);  
       
            // 3.創(chuàng)建元素  
            Element element2 = newElement("employee");  
       
            // 3.1 設(shè)置元素的屬性名及屬性值  
            element2.setAttribute(newAttribute("id", "0002"));  
       
            // 3.2 創(chuàng)建元素的屬性名及屬性值  
            Attribute name2Attr = newAttribute("name", "fox");  
       
            // 3.3 設(shè)置元素名及文本  
            Element sex2Ele = newElement("sex");  
            sex2Ele.setText("f");  
            // 設(shè)置到上層元素上  
            element2.addContent(sex2Ele);  
       
            // 設(shè)置元素  
            Element age2Ele = newElement("age");  
            age2Ele.setText("21");  
            element2.addContent(age2Ele);  
       
            // 設(shè)置為根元素的子元素  
            employees.addContent(element2);  
            // 將元素屬性設(shè)置到元素上  
            element2.setAttribute(name2Attr);  
       
            Element element3 = new Element("employee");  
            element3.setText("title");  
            element3.addContent(newElement("name").addContent(new Element("hello")));  
            employees.addContent(element3);  
       
            // 設(shè)置xml文檔輸出的格式  
            Format format =Format.getPrettyFormat();  
            XMLOutputter out = newXMLOutputter(format);  
            // 將得到的xml文檔輸出到文件流中  
            try {  
                out.output(document, newFileOutputStream(file));  
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
       
        /** 
         * 利用JDom進行xml文檔的讀取操作 
         */  
        public void parserXml(File file) {  
            // 建立解析器  
            SAXBuilder builder = new SAXBuilder();  
            try {  
                // 將解析器與文檔關(guān)聯(lián)  
                document = builder.build(file);  
            } catch (JDOMException e1) {  
                e1.printStackTrace();  
            } catch (IOException e1) {  
                e1.printStackTrace();  
            }  
            // 讀取根元素  
            Element root =document.getRootElement();  
            // 輸出根元素的名字  
            System.out.println("<" +root.getName() + ">");  
       
            // 讀取元素集合  
            List<?> employeeList =root.getChildren("employee");  
            for (int i = 0; i <employeeList.size(); i++) {  
                Element ele = (Element) employeeList.get(i);  
                // 得到元素的名字  
                System.out.println("<"+ ele.getName() + ">");  
       
                // 讀取元素的屬性集合  
                List<?> empAttrList =ele.getAttributes();  
                for (int j = 0; j <empAttrList.size(); j++) {  
                    Attribute attrs = (Attribute)empAttrList.get(j);  
                    // 將屬性的名字和值 并 輸出  
                    String name = attrs.getName();  
                    String value = (String)attrs.getValue();  
                    System.out.println(name +"=" + value);  
                }  
                try {  
                    Element sex =ele.getChild("sex");  
                   System.out.println("<sex>" + sex.getText());  
                    Element age =ele.getChild("age");  
                   System.out.println("<age>" + age.getText());  
                } catch (NullPointerException e) {  
                   System.out.println(ele.getTextTrim());  
                    Element name =ele.getChild("name");  
                   System.out.println("<name>" + name.getName());  
                     
                }  
                System.out.println("</employee>");  
            }  
           System.out.println("</employees>");  
        }  
       
        /** 
         * 測試 
         */  
        public static void main(String[] args) {  
       
            JdomDemo jdom = new JdomDemo();  
            File file = newFile("E://jdom.xml");  
            jdom.createXml(file);  
            jdom.parserXml(file);  
        }  
    }  

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇: NSData 與 NSString,Byte數(shù)組,UIImage 的相互轉(zhuǎn)換

下一篇:iOS裁剪圖片大小改變圖片尺寸圖片壓縮