国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java將數據寫入xml文件

2019-11-06 08:56:31
字體:
來源:轉載
供稿:網友
package com.lhx.test; import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException; import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Attr;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Text;  public class Test {    public static void main(String[] args) {        DocumentBuilderFactory  fct=DocumentBuilderFactory.newInstance();        try {            DocumentBuilder bui=fct.newDocumentBuilder();            Document doc=bui.newDocument();            Element ps=doc.createElement("persons");            Element p1=doc.createElement("person");            Element p2=doc.createElement("person");            Attr id1=doc.createAttribute("id");            Attr id2=doc.createAttribute("id");            id1.setNodeValue("1");            id2.setNodeValue("2");            Element name1=doc.createElement("name");            Text na1=doc.createTextNode("龍大哥");            Element name2=doc.createElement("name");            Text na2=doc.createTextNode("龍大爺");            Element sex1=doc.createElement("sex");            Text se1=doc.createTextNode("帥哥");            Element sex2=doc.createElement("sex");            Text se2=doc.createTextNode("妹子");                         doc.appendChild(ps);                ps.appendChild(p1);                    p1.appendChild(name1);                        p1.setAttributeNode(id1);                        name1.appendChild(na1);                    p1.appendChild(sex1);                        sex1.appendChild(se1);            ps.appendChild(p2);                    p2.appendChild(name2);                        p2.setAttributeNode(id2);                        name2.appendChild(na2);                    p2.appendChild(sex2);                        sex2.appendChild(se2);            try {                FileOutputStream fos=new FileOutputStream(new File("E:/longdada.xml"));                                 try {                    ((org.apache.crimson.tree.XmlDocument)doc)                    .write(fos);                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.PRintStackTrace();                }                try {                    fos.flush();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                try {                    fos.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                                              } catch (FileNotFoundException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }                                                                         } catch (ParserConfigurationException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }             }}

附圖:

參考地址:https://zhidao.baidu.com/question/305096461455202084


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永修县| 正阳县| 精河县| 隆尧县| 射阳县| 舞钢市| 巨野县| 繁峙县| 长汀县| 东阿县| 保康县| 和林格尔县| 海林市| 海安县| 临江市| 包头市| 赤峰市| 灵武市| 平罗县| 南城县| 霍林郭勒市| 新乐市| 沾益县| 玉环县| 江西省| 达州市| 兴海县| 辉南县| 富锦市| 汪清县| 连江县| 卢湾区| 勃利县| 富源县| 涞水县| 竹溪县| 梅州市| 桐梓县| 轮台县| 大洼县| 苏州市|