This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Write XML to a file

 
Steve Wood
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Md Fizal
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic