[英]Avoid writing in output file using Transformer in XML
我想将转换后的 XML 存储在内存中。
这是我的代码片段
public static void xmlProcessor(String stylesheetPathname, String inputPathname, String outputPathname ) throws TransformerException {
TransformerFactory factory = TransformerFactory.newInstance();
Source stylesheetSource = new StreamSource(new File(stylesheetPathname).getAbsoluteFile());
Transformer transformer = factory.newTransformer(stylesheetSource);
Source inputSource = new StreamSource(new File(inputPathname).getAbsoluteFile());
Result outputResult = new StreamResult(new File(outputPathname).getAbsoluteFile());
transformer.transform(inputSource, outputResult);
}
transformer.transform(inputSource, outputResult)
; 此调用将输出 XML 写入文件。 我想知道,有什么办法可以将这个转换后的 XML 存储在任何对象中,并稍后在我的代码中使用它。
对象存储(这就是我想存储转换后的 XML 的地方)
protected static Map<String, Object> transformedXMLMap = new HashMap<String, Object>();
我想这样做,这样我就可以避免对转换后的 XML 进行 IO 操作。 一旦使用,我将清空地图。
任何建议请..!
看看javax.xml.transform.dom.DOMResult
类型。
您还可以将序列化的 XML 结果存储在字符串变量中:
StringWriter sw = new StringWriter();
Result outputResult = new StreamResult(sw);
transformer.transform(inputSource, outputResult);
String lexicalXmlResult = sw.toString();
当然,您可以将String
存储在地图中,但我不确定您为什么要这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.