aspose file tools*
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Write XML to a file" Watch "Write XML to a file" New topic
Author

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());
xmlOut.append(oElement.toString());
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.
Steve
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:
<class>
<type>
MyClass
</type>
<attribute>
attr
</attribute>
<method>
myMethod
</method>
</class>
Your (pseudo)code would be:
Stringbuffer sb = new StringBuffer();
sb.append("<class>")
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);
serXML.serialize(doc);
-------------------
Here 'doc' is the DOM object.
Steve Wood
Ranch Hand

Joined: Jan 08, 2003
Posts: 137
Hi,
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
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.
Cheers,
Steve
Cindy Glass
"The Hood"
Sheriff

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
 
jQuery in Action, 2nd edition
 
subject: Write XML to a file