• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

JDOM Issue.... Please Help..

 
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

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...

versionWithdrawalXML.setRootElement(rootElement);
xmlOutputter.output(versionWithdrawalXML, fileOutputStream);

I get the output XML file filled with nodes and tags from the input file and comment at the end of the file.

Could you please help me out with this situation.


 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My Code is

 
Marshal
Posts: 27211
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Paul Clapham
Marshal
Posts: 27211
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the API documentation for the addContent() method says

Appends the child to the end of the content list.



So you shouldn't be surprised that when you use that to add a comment, the comment comes out last. If you want it to be the first child of the root element, there's a way to do that in JDOM.
 
Somnath Mallick
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I want it to come before the root node! That's why I called the setComment method in the beginning and it didn't work!! Is there a way to do that. If there is, please let me know.

I think I need a JDOM tutorial from you Paul!!!
 
Paul Clapham
Marshal
Posts: 27211
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I don't know much about JDOM. I have never used it. All I know is that it allows you to create and manage XML documents. So it's down to a knowledge of XML.

And as I said, an XML document can't have anything outside its root element. So what you're trying to create, you can't create it in JDOM or in any other DOM.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(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.

Bill
 
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
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic