欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

dom4j 生成完整xml标签内容

程序员文章站 2022-07-13 15:49:52
...
dom4j生成xml,如果标签内容为空,会简化便签,例如  <name />
但是会影响加签验签,需要生成完整的标签 <name></name>

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();