解决XML空结点⾮标准格式问题
解决XML空结点格式问题:
如有的XML标准空结点如:<GNo></GNo>要求⽣成<GNo/>的形式。利⽤DOM4J的document.asXML⽅法解决。
/**
* @Title: marshaller
* @Description: ⽣成XML报⽂,并解决空节点问题:如<GNo></GNo>改为<GNo/>
* @param @param marobjectdom4j读取xml
* @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;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论