• 从java程序写入一个xml文件


    • 步骤 

    1.通过DocumentHelper生成一个Document对象

    2.添加根元素

    3.为根元素添加子元素

    4.为某个元素添加属性,(可有可无)

    5.给某个元素添加子元素(这个可以迭代完成)

    6.给子元素添加文本信息

    7.将Document对象的内容输出到一个文件中,并命名为XML文件,

    package bjsxt.xml;
    
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.Writer;
    
    import org.dom4j.Document;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    
    public class WriteXML {
    
        public static void main(String[] args) throws IOException
        {
            //使用DocumentHelper创建一个Document对象
            Document doc = DocumentHelper.createDocument();
            //添加并得到根元素
            Element root =  doc.addElement("books");
            //为根元素添加子元素
            Element book = root.addElement("book");
            //为子元素添加属性
            book.addAttribute("id", "b01");
            //为book添加子元素
            Element name = book.addElement("name");
            Element author = book.addElement("author");
            Element price = book.addElement("price");
            
            //为子元素添加文本信息
            name.addText("Think in Java");
            author.addText("MIKE");
            price.addText("20.5");
            
            //将Document对象的内容写入到一个文件中
            Writer write = new FileWriter(new File("src/book1.xml"));
            doc.write(write);
            
            //关闭流
            write.close();
        }
    }

    运行结果:

    <?xml version="1.0" encoding="UTF-8"?>
    <books><book id="b01"><name>Think in Java</name><author>MIKE</author><price>20.5</price></book></books>

    由于以上结果的结构是不太美好,所以在输出到文件中的过程中需要修改,即以下代码

    package bjsxt.xml;
    
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.Writer;
    import java.sql.SQLException;
    
    import javax.sql.RowSetInternal;
    import javax.sql.rowset.WebRowSet;
    import javax.sql.rowset.spi.XmlWriter;
    
    import org.dom4j.Document;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.XMLWriter;
    
    public class WriteXML {
    
        public static void main(String[] args) throws IOException
        {
            //使用DocumentHelper创建一个Document对象
            Document doc = DocumentHelper.createDocument();
            //添加并得到根元素
            Element root =  doc.addElement("books");
            //为根元素添加子元素
            Element book = root.addElement("book");
            //为子元素添加属性
            book.addAttribute("id", "b01");
            //为book添加子元素
            Element name = book.addElement("name");
            Element author = book.addElement("author");
            Element price = book.addElement("price");
            
            //为子元素添加文本信息
            name.addText("Think in Java");
            author.addText("MIKE");
            price.addText("20.5");
            
            /*//将Document对象的内容写入到一个文件中
            Writer write = new FileWriter(new File("src/book1.xml"));
            doc.write(write);
            
            //关闭流
            write.close();
            */
            
            //格式良好地输出
            OutputFormat format =  OutputFormat.createPrettyPrint();
            XMLWriter write =  new XMLWriter(new FileWriter(new File("src/book2.xml")),format);
            write.write(doc);
            //关闭流
            write.close();
        }
    }
    
    

    <?xml version="1.0" encoding="UTF-8"?>

    
    

    <books>
    <book id="b01">
    <name>Think in Java</name>
    <author>MIKE</author>
    <price>20.5</price>
    </book>
    </books>

     
  • 相关阅读:
    Windows Azure Web Site (19) Azure Web App链接到VSTS
    Windows Azure Virtual Machine (35) Azure VM通过Linked DB,执行SQL Job
    Azure PowerShell (16) 并行开关机Azure ARM VM
    Windows Azure Virtual Network (12) 虚拟网络之间点对点连接VNet Peering
    Azure ARM (21) Azure订阅的两种管理模式
    Windows Azure Platform Introduction (14) 申请海外的Windows Azure账户
    Azure ARM (20) 将非托管磁盘虚拟机(Unmanage Disk),迁移成托管磁盘虚拟机(Manage Disk)
    Azure ARM (19) 将传统的ASM VM迁移到ARM VM (2)
    Azure ARM (18) 将传统的ASM VM迁移到ARM VM (1)
    Azure Automation (6) 执行Azure SQL Job
  • 原文地址:https://www.cnblogs.com/qinzhihaovip/p/9865649.html
Copyright © 2020-2023  润新知