----- 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.

James


_________________________________________________________
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

Reply via email to