How to convert a string to a domsource in Java?
•
Java
I need some help In my string filedata variable, I store an XmlDocument Now I want to convert this variable to domsource type and use this Code:
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = db.parse( new InputSource( new StringReader( filedata ) ) ); DOMSource source = new DOMSource(doc);
And through javax xml. transform. Transformer to convert:
Transformer transformer = XMLTransformerFactory.getTransformer(messageType); StreamResult res = new StreamResult(flatXML); transformer.transform(source,res);
But my flatxml is empty after conversion I checked my doc variable, which contains my XML document and parsed everything If I change my source code to the real path, everything is normal and working properly:
Source source = new StreamSource("c:\\temp\\log\\SMKFFcompleteProductionPlan.xml");
I think my problem lies in this line of code:
DOMSource source = new DOMSource(doc);
But I don't know how to solve this problem
Solution
Why do you want to build a domsource? If all you want is to provide a source of transformation input, it is more efficient to provide a streamsource, which you can do
new StreamSource(new StringReader(fileData))
It is better to also provide systemid Building DOM is a waste of time
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码