zoukankan      html  css  js  c++  java
  • java-w3c.document生成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();
            }  
            
        }
        
  • 相关阅读:
    (转)获取枚举属性的值
    C#调用C++类(以COM组件的形式)
    托管DirectX,从MDX到SlimDX的转换(转)
    数字地球影像服务后台数据读取粗糙问题
    DevExpress控件EditValue_Changed事件(延迟问题)
    Visual Studio 2008经常性卡死的其中一种解决办法
    It's not too late to start!
    Gcc编译器 linux
    UNIX系统中的进程 linux
    图算法套汇问题 linux
  • 原文地址:https://www.cnblogs.com/hwaggLee/p/5807194.html
Copyright © 2011-2022 走看看