经过一番努力,我将内容合并到一个 xml 文件中:) 以下方法删除了 xml 编码节点并将 xml 文件字符串化。
public String stringifyXmlFiles(String fileName) {
File file = new File(fileName);
String content = "";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new FileInputStream(file));
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
aTransformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
"yes");
aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
Source src = new DOMSource(document);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Result dest = new StreamResult(stream);
aTransformer.transform(src, dest);
content = stream.toString();
} catch (ParserConfigurationException e) {
logger.error(e.getMessage(), e);
} catch (SAXException e) {
logger.error(e.getMessage(), e);
} catch (IOException e) {
logger.error(e.getMessage(), e);
} catch (TransformerConfigurationException e) {
logger.error(e.getMessage(), e);
} catch (TransformerException e) {
logger.error(e.getMessage(), e);
}
return content;
}