aspose file tools*
The moose likes XML and Related Technologies and the fly likes String to XML Conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "String to XML Conversion" Watch "String to XML Conversion" New topic
Author

String to XML Conversion

Santhosh Kumar Alagiamanavalan
Greenhorn

Joined: Sep 25, 2006
Posts: 1
Hi,

I am doing a project, in which a string fetched from the database has to be converted into a XML file. For Eg the string will be like this <root><name>santhosh</name></root> and the output i need is a file like this
<root>
<name>
santhosh
</name>
</root>
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I'm a bit puzzled. Both examples you give are valid XML.

Is it just that you wish to add some line breaks? If so, can you tell us more about which circumstances you wish to add line breaks - but bear in mind that if you are not careful, adding or changing characters in an XML file can have subtle effects on the data.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Just to check for the validity (rather the well formness of the xml)copy and paste the xml in a text file and try opening in IE.If it opens then its wellformed.

The easiest way to meet your requirement.

Read the xml string into a dom and using xslt's transformer to write this to a file.Use Domsource class to wrap the org.w3c.Document object(that is your dom/doc).

I do not think you need the code for this.


;)


Rahul Bhattacharjee
LinkedIn - Blog
vinay kumar
Ranch Hand

Joined: Sep 23, 2006
Posts: 31
Incase you need code:

import javax.xml.parsers.DocumentBuilder;
import java.io.ByteArrayInputStream;
import org.w3c.dom.Document;

String s="<hi>hello</hi>";

// create an input stream
ByteArrayInputStream xmlIn = new ByteArrayInputStream(s.getBytes());

// get a document builder from the pool
DocumentBuilder builder = getBuilder();

try {

// use the DocumentBuilder to parse the XML input.
Document doc = builder.parse(xmlIn);
}
catch (Exception e) {
throw e;
}


doc is an xml document of the string provided.


regards
vnay.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by vinay kumar:
Incase you need code:

import javax.xml.parsers.DocumentBuilder;
import java.io.ByteArrayInputStream;
import org.w3c.dom.Document;

String s="<hi>hello</hi>";

// create an input stream
ByteArrayInputStream xmlIn = new ByteArrayInputStream(s.getBytes());

// get a document builder from the pool
DocumentBuilder builder = getBuilder();

try {

// use the DocumentBuilder to parse the XML input.
Document doc = builder.parse(xmlIn);
}
catch (Exception e) {
throw e;
}


doc is an xml document of the string provided.


regards
vnay.


Vinay : Code seems to be incomplete.This is half way.The original requirement is to write the xml dom to a file.In that case the below mentioned code might be useful.(for converting a DOC to xml string)

StringWriter output = new StringWriter();
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(dom), new StreamResult(output));

now write output.toString() to the FileOutputStream.

File f = new File("PATH");
FileOutputStream fout = new FileOutputStream(f);
fout.write(output.toString().getBytes());

This complets the code for the requirement.

Hope this helps.
 
 
subject: String to XML Conversion