David,
dom4j behaved correctly, since you are requesting wrong element from the context of your XPath.
You are retrieving <first> element in this line of the code:
> Node node = document.selectSingleNode("/root/first");
Then you create a copy of that node.
> elem = elem.createCopy();
At that point you only have <first> element, i.e. if you try this:
elem.valueOf("..");
You will get null. Your context at the point when you create the copy becomes <first> element, and it has no parent node (this is why you get nothing when you call elem.getParent()).
If you do this, you'll also get 'Dave':
System.out.println("str = " + elem.valueOf("/first/last"));
I hope this helps you.
Best regards
Ben
P.S.
To check if your XPath _expression_ is correct for what you require try using XPath Explorer by Purple Technologies:
http://www.purpletech.com/xpe/
On Fri, 2003-12-12 at 04:20, David Thielen wrote:
Hi;
Element.createCopy() is not handling XPath as I would expect - it won't accept a "/base" as an argument - it requires "./base"
The code:
public class Dom4jTest {
public static void main(String[] args) throws DocumentException {
System.out.println("starting");
Document document = DocumentHelper.parseText("<root><first><last>dave</last></first></root>");
System.out.println("document = " + document);
Node node = document.selectSingleNode("/root/first");
System.out.println("node = " + node);
System.out.println("str = " + node.valueOf("/root/first/last"));
Element elem = (Element) node;
System.out.println("elem = " + elem);
elem = elem.createCopy();
System.out.println("new elem = " + elem);
System.out.println("new parent = " + elem.getParent());
System.out.println("str = " + elem.valueOf("/last"));
System.out.println("str = " + elem.valueOf("./last"));
System.out.println("all done");
}
}
produces:
starting
document = [EMAIL PROTECTED] [Document: name null]
node = [EMAIL PROTECTED] [Element: <first attributes: []/>]
str = dave
elem = [EMAIL PROTECTED] [Element: <first attributes: []/>]
new elem = [EMAIL PROTECTED] [Element: <first attributes: []/>]
new parent = null
str = <*************** this should return dave!!!!!
str = dave
all done
help - thanks - dave
--
benjamin kopic
m: +44 (0)780 154 7643
t: +44 (0)20 7794 3090
e: [EMAIL PROTECTED]
w: http://www.panContext.com/
|
|