I am new to this JDOM thing. I am trying to create a new XML file from an existing XML file with tags commented. But most of the things I have tried haven't worked as of yet. Please take a look at this code. When I try to add the initial comment and call the AddComment.setComment(versionWithdrawalXML, fileOutputStream, xmlOutputter, createdComment) method, I get the error message java.lang.IllegalStateException: Root element not set. I am just adding a new comment to the blank output file. So it should just add the comment right?? Please take a look at the code. I have two question as of now....
1. How to get rid of the above mentioned exception.
2. When I move the add comment statement below the statement...
That's a basic rule of XML. Every XML document must have exactly one root element and any other node must be a descendant of that root node. That includes comments.
So that explains the exception. As for your complaint about the comment not appearing where you want it to appear, you'll have to look at the code which adds the comment to the document. I don't believe you posted it... did you really write a separate class just to add a comment to a document?
Also, that whole section of code which deletes the file if it's already there and creates a new file in any case: that's all unnecessary. The FileWriter and FileOutputStream classes do that anyway.
Yes I wrote the code about the comments in a different class. But then how to make the comment appear in the beginning??? That's what i want to know. My code to add the comment is... I just called this method in the beginining so that the comment is added first. But I get the error above mentioned.
(repeat of a post which the system lost)
The whole point of DOM models is that everything is contained within the root so you cant add outside the root.
Therefore you will have to try something else - you could use XSLT to output comments before the root or you could write correctly formatted comments to the output stream before outputting the Document.
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
Free, earth friendly heat - from the CodeRanch trailboss