dom4j 生成完整xml标签内容

阅读更多
dom4j生成xml,如果标签内容为空,会简化便签,例如 
但是会影响加签验签,需要生成完整的标签

SAXReader saxReader = new SAXReader();
		Document document = saxReader.read(new ByteArrayInputStream(message.getBytes("UTF-8")));

		Iterator orderIt = document.selectNodes("/CMBCPayRes/MsgBody").iterator();
		Element elem = null;
		while (orderIt.hasNext()) {
			elem = (Element) orderIt.next();
		}
		
		OutputFormat xmlFormat = new OutputFormat();
		xmlFormat.setExpandEmptyElements(true);
		
		StringWriter sw = new StringWriter();
		XMLWriter xmlWriter = new XMLWriter(sw,xmlFormat);
		xmlWriter.write(elem);
		xmlWriter.close();
		
		return sw.toString();

你可能感兴趣的:(dom4j,java,格式化,完整,补全)