• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

JDOM AddException when adding nodes to an XML document

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm trying to use JDOM to convert some letters into XML. I can get the structure for the XML header, author tag and the whole letter into a tag, i.e
<author>hogarth<ln>text of the letter</ln></author> but what I want to do is to have each line attached to a node like <author>hogarth<ln>line 1</ln><ln>line 2</ln>...& so on</author>. However I keep getting the error message:
org.jdom.IllegalAddException: The Content already has an existing parent "author"
at org.jdom.ContentList.add(ContentList.java:209)
at org.jdom.ContentList.add(ContentList.java:131)
at java.util.AbstractList.add(Unknown Source)
at org.jdom.Element.addContent(Element.java:811)
at uk.co.austgate.xml.jdbcXml.toDocument(jdbcXml.java:38)

My code is:

The code is called from a Data Access Object to turn the letter text into XML to be serialised to a servlet. I'd be grateful for any suggestions or if i should be really be using something like SAX instead. Thanks in advance.
Iain
 
Sheriff
Posts: 28346
97
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
It seems to me from the text of your post that you want to have more than one <ln> element. But your code only creates one. This is obviously wrong.

And if you only want one <author> element -- I couldn't tell from the text whether that was actually a requirement or not -- then you should add it to the document element only once. Your code does it repeatedly, which looks wrong.

And no, using SAX would be absurd. SAX is a parser and you aren't parsing a document there.
 
Iain Emsley
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul. I've just realised that I'd misunderstood some of the spec but thanks picking up on the adding the element in the loop.
 
It's a beautiful day in this neighborhood - Fred Rogers. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic