zoukankan      html  css  js  c++  java
  • Dom4j写XML

    package com;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    
    import org.dom4j.Document;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.XMLWriter;
    
    public class Text {
      public static void main(String[] args) {
        //创建文档并设置文档根节点元素:第1种方式
    //    Document document=DocumentHelper.createDocument();
    //    Element root=DocumentHelper.createElement("student");
    //    document.setRootElement(root);
          
        //创建文档并设置文档根节点元素:第2种方式
        Element root=DocumentHelper.createElement("student"); 
        Document document=DocumentHelper.createDocument(root);
        //在节点内添加属性
        root.addAttribute("name", "majun");
        //创建一个新的节点hello放在root下面
        Element  helloelement1=root.addElement("hello");
        //设置节点hello的内容为hello
        helloelement1.setText("hello");
        //设置输出的格式
        OutputFormat  format=new  OutputFormat("   ",true);
        XMLWriter writerxml;
        try {
            //输出
            writerxml = new XMLWriter(new FileOutputStream("mm.xml"),format);
            writerxml.write(document);
            
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    }
  • 相关阅读:
    【应用安全】mssql db_owner权限拿shell
    【应用安全——XSS】input-hidden
    留言板
    Git配置多个SSH-Key
    13.InternalThreadLocalMap
    10.ChannelOutboundBuffer
    9.ChannelHandlerContext
    8.Future&Promise
    7.给大动脉来一刀-NioEventLoop 源码分析
    6.给大动脉来一刀
  • 原文地址:https://www.cnblogs.com/michaeljunlove/p/3906598.html
Copyright © 2011-2022 走看看