gpt4 book ai didi

java - 避免使用 XML 中的 Transformer 写入输出文件

转载 作者:行者123 更新时间:2023-12-01 19:16:20 25 4
gpt4 key购买 nike

我想将转换后的 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 操作。一旦使用,我就会清空 map 。

任何建议请..!

最佳答案

查看 javax.xml.transform.dom.DOMResult 类型。

关于java - 避免使用 XML 中的 Transformer 写入输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59412781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com