解决XML空结点非标准格式问题

解决XML空结点格式问题:

   如有的XML标准空结点如:<GNo></GNo>要求生成<GNo/>的形式。利用DOM4J的document.asXML方法解决。

/** 
    * @Title: marshaller 
    * @Description: 生成XML报文,并解决空节点问题:如<GNo></GNo>改为<GNo/>
    * @param @param marobject
    * @param @param mappingfile
    * @param @param fileName
    * @param @return
    * @param @throws Exception   
    * @return String  
    * @throws 
    */ 
    public static String marshaller(Object marobject, String mappingfile,String fileName) throws Exception {
        String objStr = "";
        try {
            Mapping map = new Mapping();
            map.loadMapping(mappingfile);
            if (true) {
                StringWriter sw = new StringWriter();
                Marshaller marshaller = new Marshaller(sw);
                marshaller.setEncoding("UTF-8");
                marshaller.setMapping(map);
                marshaller.marshal(marobject);
                sw.flush();
                objStr = sw.toString();
                
                FileOutputStream fo = new FileOutputStream(new File(fileName));
                PrintStream so = new PrintStream(fo,true,"UTF-8");
            	Document document = DocumentHelper.parseText(objStr);
                so.print(document.asXML());
            	
                sw.close();
            }
        } catch (Exception ex) {
            throw new Exception("marshaller() " + ex.toString());
        }
        return objStr;
    }

 

你可能感兴趣的:(解决XML空结点非标准格式问题)