Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Current context for dom4j

 
Douglas Kent
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I'm using dom4j to construct an XML document from a series of records. I need to be able to start searching the document at a particular place as I'm loading in the various elements. Once found, I need to be able to create a new element at that location. I've set up a series of records like so:
ELEMENT TEXT
library.book "Miles Copperthwaith"
library.book.chapter "I Am Born"

The algorithm:
Starting from an empty document, create the root element.
Set the context at the root
Cycle thru the elements, searching for the element starting at the current context.
If not found, create the element AT THAT POINT IN THE CONTEXT.
If found, set the context to that element and look for the next element, unless you are at the last element, in which case
you create the element at that point, the current context, and add the text.

It's simple, but you get the idea. How to set the current context, and create an element at the right place, using the current context?
 
Paul Clapham
Sheriff
Posts: 21133
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds to me like something simple like a variable named "context" would do it. Otherwise I don't understand what you're getting at.
 
Douglas Kent
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, and what would that variable hold?
 
Paul Clapham
Sheriff
Posts: 21133
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it would hold a reference to the node which you consider to be the "context" at that point in time.
 
Douglas Kent
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if you find an element using some search, add an element to that element, the document is updated, because the element is passed back by reference, correct?
 
Paul Clapham
Sheriff
Posts: 21133
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what it means to "pass back an element by reference". You do have a reference to an element, if that's what you had in mind. But there's nothing unusual about that, because in Java you only ever have references to objects anyway. There's nothing special about objects generated by dom4j.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic