简体   繁体   中英

Avoid writing in output file using Transformer in XML

I want to store my transformed XML in memory.

Here is my code snippet

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) ; this call is writing the output XML in file. I want to know , Is there any way I could store this transformed XML in any object and use it later in my code.

Object storage (That's Where I'm thinking to store my transformed XML)

protected static Map<String, Object> transformedXMLMap = new HashMap<String, Object>();

I want to do it, so that I could avoid IO operation on transformed XML. And once utilized, I'll empty the map.

Any suggestions please..!

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

You can also store the serialized XML result in a string variable:

StringWriter sw = new StringWriter();
Result outputResult = new StreamResult(sw);
transformer.transform(inputSource, outputResult);
String lexicalXmlResult = sw.toString();

and of course you can store the String in a map, but I'm not sure why you want to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM