Hi guys,

I'm a bit clueless here - Christian - where is this miraculous Dom4jList
that you are talking about (near the end of your message)? If it is truly
there, it would probably spare a lot of programmers some headache (although
it wouldn't be too difficult to implement it ourselves - I'd like to know if
it's already there). And moreover, I have been thinking about it and I agree
with James that: If one is to extract a list of elements from a DOM
document, and then add an element in the middle, then one had better fish
out the reference of the node before which one needs to add the new element
(or its parent):
e.g.:

import org.w3c.dom.*;
import org.dom4j.dom.*;

List oNodeList = oDoc.selectNodes("//foo");
Node oRefNode = (Node)oNodeList.get(3);

//By the reference of the node itself
Node oGuest = oDoc.createElement("newfoo");
oDoc.insertBefore(oGuest, oRefNode); //Will become a preceding sibling of
oRefNode

//Otherwise, by the reference of the parent node
Node oGuest = oDoc.createElement("newfoo");
Node oParent = ((Node)oNodeList.get(3)).getParentNode();
oParent.appendChild(oGuest); //Will become the last sibling of oRefNode

Now that James has rectified the bug in insertBefore(), I think the first
method should be most convenient (I hope James has made sure that if the
oRefNode happens to be the first child of its parent, then inserting Before
should make the new node the first child, and not its parent or something
else). A wrapper class like Christian's Dom4jList may be very useful if the
4 methods that he described are indeed implemented in it. 

Well, I think that's probably enough for me to understand this whole thing.
Thanks to you both for this long comprehensive discussion.
Cheerio
Soumanjoy


-----Original Message-----
From: James Strachan [mailto:[EMAIL PROTECTED]]
Sent: Thursday, April 25, 2002 9:49 PM
To: Christian Holmqvist, IT, Posten; [EMAIL PROTECTED]
Subject: Re: [dom4j-user] Node List returned by selectNodes()


Hi Christian.

It could be that we could return an immutable List from the selectNodes()
method via...

Collections.unmodifiableList( list );

in java.util.Collections.


Though so long as the documentation is clear that modifications to the List
will not be reflected in the document I think I'd rather not add this extra
complication, both from efficiency and to allow end users to modify the list
(e.g. to sort the list or filter it further).

James

From: "Christian Holmqvist, IT, Posten" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, April 25, 2002 1:41 PM
Subject: SV: [dom4j-user] Node List returned by selectNodes()

-----Original Message-----
From: Christian Holmqvist, IT, Posten
[mailto:[EMAIL PROTECTED]]
Sent: Thursday, April 25, 2002 6:12 PM
To: [EMAIL PROTECTED]
Subject: SV: [dom4j-user] Node List returned by selectNodes()


Scroll dooooown...

> -----Ursprungligt meddelande-----
> Fr�n: James Strachan [mailto:[EMAIL PROTECTED]]
> Skickat: den 25 april 2002 13:57
> Till: Christian Holmqvist, IT, Posten; 'Soumanjoy Das';
> [EMAIL PROTECTED]
> �mne: Re: [dom4j-user] Node List returned by selectNodes()
> 
> 
> ----- Original Message -----
> From: "Christian Holmqvist, IT, Posten" 
> <[EMAIL PROTECTED]>
> > First I hop you all don't mind if I move this discussion to 
> the mailing
> > list, this is far to intreressting not to be included there.
> 
> Not at all.
> 
> > > From: "Soumanjoy Das" <[EMAIL PROTECTED]>
> > > > Hi James,
> > > >
> > > > This is just a very elementary question where Christian and
> > > I are having
> > > > some confusion. I want to know this so as to clarify my
> > > concepts: When a
> > > > list of nodes is returned by .selectNodes(), and then I add
> > > a Node to the
> > > > List object, does that add the Node to the original 
> document too?
> > >
> > > No absolutely not. e.g where should it add it? I could do
> > >
> > > List foos = doc.selectNodes( "//foo" );
> > >
> > > and find all kinds of different elements. Adding a new node
> > > to the list,
> > > where should it go?
> > >
> > > I agree though maybe it should be readonly - but sometimes
> > > people want to
> > > sort the results and so forth.
> > >
> > > If you want to add to the tree, find a node in the results
> > > you like and add
> > > it to its parent.
> > >
> > > Node node = (Node) foos.get(0);
> > > Element parent = node.getParent();
> > > parent.addElement( "somethingElse" );
> >
> > Oki, this is all fine BUT
> >
> > what happens if we do:
> > List foos = doc.selectNodes("//foo");
> > foos.add(1,doc.createElement("newElement"));
> > doc.setContent(foos);
> >
> > Where does the "newElement" go?
> 
> That would kinda work.
> 
> Though the problem is the other <foo> elements in the list could well
> already have different parents - so an exception might well 
> occur because
> you're trying to add a node which is already a child of 
> another element.
> 
> So you could iterate through the list and call node.detach() 
> on them to
> prune them from their place in the document, then the above 
> would work.
> 
> Oh and one more thing to remember; a document can only have 1 
> root element.
> So an exception will occur when trying to add the 2nd element. If
> 'doc.setContent()' were applied on an Element, this would be better.

So the setContent method should be in the element class and not the branch
class since setContent takes a list and a list implies more the one node.
And since the list can not show how the nodes should be added (level or
order) in relation to eachother. I.e.  they are added with
while(list.hasNext())
{
        Node n = (Node)list.next();
        n.detach();
        doc.add(n);
}


Ok... but my view of a result from a xpath selection is different.
The result is only a referens list to the elements that is the result from
the xpath expression. This implies that nothing can be added to the list
since the elements already contain a order within the document. Or if there
is adding to be done the position in the document also has to be specified. 
Example xml: (thanks to Soumanjoy for this example xml *smile*)
<Root>
        <A>
                <B>
                        <A/>
                </B>
        </A>
        <C>
                <A>
                        <B>
                                <A/>
                        </B>
                </A>
        </C>
</Root>

Dom4jList foo = doc.selectNodes("//B");

There is two nodes in the Dom4jList now.

The Dom4jList is almost the same as the List class except it don't support
the 
.add(int,object) method instead there is foure other that is directly
working on the document.

foo.addBefore(0, doc.createElement("X"));
Result:
<Root>
        <A>
                <X/>
                <B>
                        <A/>
                </B>
        </A>
        <C>
                <A>
                        <B>
                                <A/>
                        </B>
                </A>
        </C>
</Root>

foo.addAfter(0,doc.createElement("Y"));
Result:
<Root>
        <A>
                <B>
                        <A/>
                </B>
                <Y/>
        </A>
        <C>
                <A>
                        <B>
                                <A/>
                        </B>
                </A>
        </C>
</Root>

foo.addInside(0,doc.createElement("Z"));
Result
<Root>
        <A>
                <B>
                        <Z/>
                        <A/>
                </B>
        </A>
        <C>
                <A>
                        <B>
                                <A/>
                        </B>
                </A>
        </C>
</Root>

foo.addAround(0,doc.createElement("T"));
Result
<Root>
        <A>
                <T>
                        <B>
                                <A/>
                        </B>
                </T>
        </A>
        <C>
                <A>
                        <B>
                                <A/>
                        </B>
                </A>
        </C>
</Root>

Ohh well I seems like I floated away again... *hihi*

/Christian

_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to