aspose file tools*
The moose likes XML and Related Technologies and the fly likes Current context for dom4j Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Current context for dom4j" Watch "Current context for dom4j" New topic
Author

Current context for dom4j

Douglas Kent
Ranch Hand

Joined: Oct 06, 2000
Posts: 171
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
Bartender

Joined: Oct 14, 2005
Posts: 18988
    
    8

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

Joined: Oct 06, 2000
Posts: 171
OK, and what would that variable hold?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18988
    
    8

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

Joined: Oct 06, 2000
Posts: 171
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
Bartender

Joined: Oct 14, 2005
Posts: 18988
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Current context for dom4j