File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes Write XML to a file 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 "Write XML to a file" Watch "Write XML to a file" New topic

Write XML to a file

Steve Wood
Ranch Hand

Joined: Jan 08, 2003
Posts: 137
Hi guys,
I'm having problems with writing XML out to a string/file. My code used to work with J2SE but doesn't work anymore with J2EE (Borland). Basically, when I use the code below, I get:
<Design : null>
I am trying to write an XML document to a file using a StringBuffer: (I've only included the bit that builds the actual string, not the file writing)
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.w3c.dom.*;
public static StringBuffer writeDesign(Design oDesign)
StringBuffer xmlOut = new StringBuffer();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document docDesign = db.newDocument();
xmlOut.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
Element oElement = docDesign.createElement("Design");
oElement.setAttribute("Name", oDesign.getName());
oElement.setAttribute("Description", oDesign.getDescription());
oElement.setAttribute("Remarks", oDesign.getRemarks());
return xmlOut;
Am I going about this completely the wrong way or am I missing something really simple? There doesn't seem to be much information on writing XML only reading it.
Any help is greatly appreciated.
rom chatterjee
Ranch Hand

Joined: Dec 11, 2001
Posts: 46
I suppose it depends what you are using as your source for the final file. If it is a document in memory (DOM), then you can walk the tree building the StringBuffer as you go by having methods for handling elements, attributes, comments, etc..
If, however, your source is something else, then you must decide which parts of the source make up the final document and construct the StringBuffer accordingly.
For example say you decided to build an xml representation of a class you could (and Im not suggesting this is the right way) aim for a document that will look like:
Your (pseudo)code would be:
Stringbuffer sb = new StringBuffer();
sb.append("<type>" + getClassName() + "</type>");
So you would have to rembember to add your opening and closing brackets in the right place and so on.
You may want to try a google search for some pretty printers relating to XML documents, as you may find someone else has done this before
Hope this helps

Rom Chatterjee<BR>Sun Certified Java Programmer
Md Fizal
Ranch Hand

Joined: Dec 23, 2002
Posts: 61
Try this
FileOutputStream f = new FileOutputStream("a.xml");
XMLSerializer serXML = new XMLSerializer(f, null);
Here 'doc' is the DOM object.
Steve Wood
Ranch Hand

Joined: Jan 08, 2003
Posts: 137
Thanks for the information. I think I'm going to go for the first option (I've already written most of the XML writing code and I like the way I've written the XML).
Only problem is:
How do I make sure none of the characters will interfere with the XML parser?
I've just noticed that '�' is a reserved character. I've obviously already changed '<' and '>' to '<' and '>' respectively, but I'm sure there are loads more to consider.
Does anyone know of a useful document to read on the subject?
Cheers for your help,
Steve Wood
Ranch Hand

Joined: Jan 08, 2003
Posts: 137
I've found the problem.
The characters are fine if I change my character set to "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" instead of the usual "<?xml version=\"1.0\" encoding=\"UTF-8\"?>".
I'm not sure if this is getting me into dodgy territory, but it solves the problem.
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Well now that you have solved all your own problems, I am going to transfer this topic to the XML forum, so that they can share your wisdom .

"JavaRanch, where the deer and the Certified play" - David O'Meara
I agree. Here's the link:
subject: Write XML to a file
It's not a secret anymore!