How to convert a string to a xml in java?

boxcarracer8792

New Member
I have a string object "hello world"I need to create an xml file from this string with hello world as text content.I tried the following code snippet\[code\]String xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"></soap:Envelope>"; 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/11465567/new DOMSource( document ); Result dest = new StreamResult( new File("D:\\myXML.xml" ) ); aTransformer.transform( src, dest ); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } \[/code\]this code works fine. but when i replace the string with "Hello world" its not working.Can any one help me out in this ?Thanks
 
Back
Top