以DOM方式创建xml文件

有些事,在我这个单纯的人心里一直以为这世界也是单纯的存在。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文件
接着走你!
以DOM方式创建xml文件
搜索到你了!
以DOM方式创建xml文件
点开看看?
以DOM方式创建xml文件
嗯嗯,很规范嘛。来,吃个鸡然后听歌睡觉。

猜你喜欢

转载自blog.51cto.com/13479739/2170430