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() 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 _________________________________________________________ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com _______________________________________________ dom4j-user mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/dom4j-user
