java-w3c.document生成xml文件

本文介绍了一种使用Java创建并写入XML文件的方法,包括解析器配置、DOM对象创建、元素添加及最终文件输出的过程。通过遍历数据结构,将信息转化为XML格式,并详细展示了如何设置XML文件的编码、版本和缩进。

案例

    /**
     * 创建和写入xml
     * @param xmlrootname
     * @param waitConverList
     */
    private void createAndWriterXML(String xmlrootname,Map<String ,BaseModel> waitConverList){
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
        DocumentBuilder builder = null ;
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        if( builder == null ) return ;
        Document  document = builder.newDocument();  
        Element root = document.createElement(xmlrootname);   //创建根节点  
        document.appendChild(root);  

        Map<String,TableXml> xmlTableMap = InitTableInfo.getXmlTableMap();
        for (String tablename: waitConverList.keySet()) {
            TableXml xml = xmlTableMap.get(tablename);
            if(xml == null )continue;
            BaseModel baseModel = waitConverList.get(tablename);
            if( baseModel == null )continue;
            HashMap<String, Object> map = ConverUtils.converBeanToMap(baseModel);
            
            Element item = document.createElement(xml.getValue());  
            root.appendChild(item);
            if( map != null && map.size()>0 ){
                Map<String,String> columnMap = xml.getColumnMap();
                for (String key : columnMap.keySet()) {
                    if(StringUtils.isBlank(key))continue;
                    Element childItem = document.createElement(columnMap.get(key));  
                    Object ob = map.get(key.toUpperCase());
                    childItem.appendChild(document.createTextNode(ob==null?"":ob.toString()));  
                    item.appendChild(childItem);
                }
            }
        }
          
        //将DOM对象document写入到xml文件中  
        TransformerFactory tf = TransformerFactory.newInstance();  
        try {  
            Transformer transformer = tf.newTransformer();  
            DOMSource source = new DOMSource(document);  
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");  
            transformer.setOutputProperty(OutputKeys.VERSION, "1.0");  
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");  
            PrintWriter pw = new PrintWriter(new FileOutputStream("C:\\Users\\huage\\Desktop\\o\\"+xmlrootname+".xml"));  
            StreamResult result = new StreamResult(pw);  
            transformer.transform(source, result);     //关键转换  
        } catch (Exception e) {  
            e.printStackTrace();
        }  
        
    }
    

 

转载于:https://www.cnblogs.com/hwaggLee/p/5807194.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值