zoukankan      html  css  js  c++  java
  • Java生成XML文件

    我们在数据库中的数据可以将其提取出来生成XML文件,方便传输。例如数据库中有Admin这张表:

    我们写一个java类表示admin数据:

     1 package xmlDom.vo;
     2  
     3 import java.io.Serializable;
     4 import java.util.Date;
     5 import java.util.List;
     6 
     7 public class Admin implements Serializable
     8 {
     9     private String aid;
    10     private String password;
    11     private Integer rid;
    12     private Integer type;
    13     private Date lastdate;
    14     private Integer flag ; 
    15      
    16     
    17  
    18  
    19     public Integer getRid()
    20     {
    21         return rid;
    22     }
    23     public void setRid(Integer rid)
    24     {
    25         this.rid = rid;
    26     }
    27     public String getAid()
    28     {
    29         return aid;
    30     }
    31     public void setAid(String aid)
    32     {
    33         this.aid = aid;
    34     }
    35     public String getPassword()
    36     {
    37         return password;
    38     }
    39     public void setPassword(String password)
    40     {
    41         this.password = password;
    42     }
    43     public Integer getType()
    44     {
    45         return type;
    46     }
    47     public void setType(Integer type)
    48     {
    49         this.type = type;
    50     }
    51     public Date getLastdate()
    52     {
    53         return lastdate;
    54     }
    55     public void setLastdate(Date lastdate)
    56     {
    57         this.lastdate = lastdate;
    58     }
    59     public Integer getFlag()
    60     {
    61         return flag;
    62     }
    63     public void setFlag(Integer flag)
    64     {
    65         this.flag = flag;
    66     } 
    67     
    68 }

    然后通过JDBC 将表中的数据转换成一个链表:

     1 package xmlDom.dbc;
     2 
     3 import java.sql.Connection;
     4 import java.sql.DriverManager;
     5 import java.sql.SQLException;
     6 
     7 public class DatabaseConnection
     8 {
     9     private static final String MYSQLDRIVER = "org.gjt.mm.mysql.Driver";
    10     private static final String MYSQLURL = "jdbc:mysql://localhost:3306/hrdb";
    11     private static final String USERNAME = "root";
    12     private static final String PASSWORD = "admin";
    13 
    14     private Connection conn;
    15 
    16     public DatabaseConnection()
    17     {
    18         try
    19         {
    20             Class.forName(MYSQLDRIVER);
    21             this.conn = DriverManager.getConnection(MYSQLURL, USERNAME, PASSWORD);
    22         } catch (Exception e)
    23         {
    24             e.printStackTrace();
    25         }
    26     }
    27 
    28     public Connection getConnection() throws Exception
    29     {
    30         if (null != this.conn)
    31             return this.conn;
    32         else
    33             throw new Exception("获取数据库连接失败");
    34     }
    35 
    36     public void close()
    37     {
    38         if (null != this.conn)
    39         {
    40             try
    41             {
    42                 this.conn.close();
    43             } catch (SQLException e)
    44             {
    45                 e.printStackTrace();
    46             }
    47         }
    48     }
    49 }
        public static List<Admin> loadFromDB() throws Exception
        {
            List<Admin> allAdmins = new ArrayList<Admin>();
            
            DatabaseConnection dbc = new DatabaseConnection();
            Connection conn = dbc.getConnection();
            PreparedStatement ps = conn.prepareStatement("SELECT aid,rid,type,lastdate,flag FROM admin");
            ResultSet rs = ps.executeQuery();
            while(rs.next())
            {
                Admin vo = new Admin();
                
                vo.setAid(rs.getString("aid"));
                vo.setRid(rs.getInt("rid"));
                vo.setType(rs.getInt("type"));
                vo.setLastdate(rs.getDate("lastdate"));
                vo.setFlag(rs.getInt("flag"));
                
                allAdmins.add(vo);
            }
            
            return allAdmins;
        }

    然后将该链表转换成内存中的XML结构:

        public static Document getDocument( List<Admin> allAdmins) throws Exception
        {
            DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = dbfactory.newDocumentBuilder();
            Document document = documentBuilder.newDocument();
            
            Element root = document.createElement("admins");
            
            for(Admin item : allAdmins)
            {
                Element admin = document.createElement("admin");
                admin.setAttribute("aid", item.getAid());
                
                Element rid = document.createElement("rid");
                rid.appendChild(document.createTextNode(String.valueOf(item.getRid())));
                
                Element type = document.createElement("type");
                type.appendChild(document.createTextNode(String.valueOf(item.getType())));
                
                Element lastdate = document.createElement("lastdate");
                lastdate.appendChild(document.createTextNode(String.valueOf(item.getLastdate())));
                
                Element flag = document.createElement("flag");
                flag.appendChild(document.createTextNode(String.valueOf(item.getFlag())));
                
                admin.appendChild(rid);
                admin.appendChild(type);
                admin.appendChild(lastdate);
                admin.appendChild(flag);
                
                root.appendChild(admin);
            }
            
            document.appendChild(root);
            
            
            
            return document;
        }

    然后将该内存的XML保存到本地中:

        public static void main(String[] args) throws Exception
        {
            List<Admin> allAdmins  = loadFromDB();
            Document document = getDocument(allAdmins);
            
            Source source = new DOMSource(document);
            StreamResult stream = new StreamResult(new FileOutputStream(new File("C:\D\code\resource\admins.xml")));
            TransformerFactory transFactory = TransformerFactory.newInstance();
            Transformer trans = transFactory.newTransformer();
            trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            trans.transform(source, stream);
            
            System.out.println("main done//~~");
        }

     也可以用下面代码把xml文件加载到内存中:

    1     public static Document loadFromFile(String path) throws Exception
    2     {
    3         Document document = null;
    4         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    5         DocumentBuilder builder = factory.newDocumentBuilder();
    6         document = builder.parse(new File(path));
    7         return document;
    8     }
  • 相关阅读:
    sqlserver 配置管理器中无法启动sqlserver服务
    9.4笔记
    ASP.NET 正则表达式验证
    ASP.NET之OnClientClick 事件
    ASP.NET之js 根据textbox变化刷新相关数据
    SQL
    leetcode:Roman to Integer(罗马数字转化为罗马数字)
    leetcode:Integer to Roman(整数转化为罗马数字)
    leetcode:Palindrome Number
    leetcode:String to Integer (atoi)
  • 原文地址:https://www.cnblogs.com/kuillldan/p/6227439.html
Copyright © 2011-2022 走看看