How to convert a String to an XML object in Java

widget.geek

New Member
I get a SOAP message from a web service, and I can convert the response string to an XML file using the below code. This works fine. But my requirement is not to write the SOAP message to a file. I just need to keep this XML document object in memory, and extract some elements to be used in further processing. However, if I just try to access the document object below, it comes as empty.Can somebody please tell me how I can convert a \[code\]String\[/code\] to an in-memory XML object (without having to write to a file)?\[code\]String xmlString = new String(data); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try { builder = factory.newDocumentBuilder(); // Use String reader Document document = builder.parse( new InputSource( new StringReader( xmlString ) ) ); TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); Source src = http://stackoverflow.com/questions/12698561/new DOMSource( document ); Result dest = new StreamResult( new File("xmlFileName.xml" ) ); aTransformer.transform( src, dest );}\[/code\]
 
Back
Top