1. 程式人生 > >以DOM方式創建xml文件

以DOM方式創建xml文件

void ima path cdc content ttext ges image cat

有些事,在我這個單純的人心裏一直以為這世界也是單純的存在。Until that moment, she paid the bill for another boy, I found out that people are together, I am just an outsider.My world is lost.好久沒扯皮了,閑扯一下很開心。好了,廢話少說,內容都在註釋裏面。

    /**以DOM方式創建xml文件
             * @param file 文件
             * @throws Exception
             */
            public static void UseDomCreateXml()throws Exception{
                //1:創建DocumentBuilder
                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                    DocumentBuilder db = dbf.newDocumentBuilder();
                    // 2:創建dom對象
                    Document document = db.newDocument();
                    document.setXmlStandalone(true);
                    // 3:創建dom中元素:根元素
                    Element root = document.createElement("different_doc_zip");
                    //4:生成子節點內容
                    for (int i = 0; i < 3; i++) {
                            Element title = document.createElement("title");
                            //5:創建子元素
                            Element doc = document.createElement("doc"),
                                            png = document.createElement("png"),
                                            xls = document.createElement("grade"),
                                            jpg = document.createElement("jpg"),
                                            txt = document.createElement("txt");
                            //6:向元素設置屬性
                            title.setAttribute("id", UUID.randomUUID().toString());
                            //7:向元素添加文本內容
                            title.setTextContent("我是文章標題");
                            doc.setTextContent("xxxxx.doc");
                            png.setTextContent("xxxxx.png");
                            xls.setTextContent("xxxxxx.xls");
                            jpg.setTextContent("xxxxx.jpg");
                            txt.setTextContent("abcdefgh.txt");
                            title.appendChild(doc);
                            title.appendChild(png); 
                            title.appendChild(xls);
                            title.appendChild(jpg);
                            title.appendChild(txt);
                            root.appendChild(title);
                    }
                    //8:把根元素添加至dom中
                    document.appendChild(root);  
                    //8:把dom對象導出到xml文件
                    //創建轉換器工廠
                    TransformerFactory tff = TransformerFactory.newInstance();
                    //由工廠創建轉換器實例
                    Transformer tf = tff.newTransformer();
                    //設置轉換格式
                    tf.setOutputProperty(OutputKeys.INDENT, "yes");
                    //由轉換器把dom資源轉換到結果輸出流,而結果輸出流連接到一個xml文件
                HttpServletRequest request = ServletActionContext.getRequest();
            String path = request.getServletContext().getRealPath("/");
            String uuid = UUID.randomUUID().toString();
            FileOutputStream output = new FileOutputStream(path + uuid + "xxxx.xml");
                    tf.transform(new DOMSource(document), new StreamResult(output));
            }

接下來,我們調用一下:

    try {
                AllServiceIsHere.UseDomCreateXml();
            } catch (Exception e) {
                e.printStackTrace();
            }

打個斷點,然後再打開強大的EveryThing看看效果
哦,叫這個名字:
技術分享圖片
接著走你!
技術分享圖片
搜索到你了!
技術分享圖片
點開看看?
技術分享圖片
嗯嗯,很規範嘛。來,吃個雞然後聽歌睡覺。

以DOM方式創建xml文件