Then XPath is your best bet. You'll want to pre-compile any XPaths you intend to keep around for awhile to save from parsing the string all the time. You might look at directly using the Jaxen interfaces, since they have some more specific functions, such as XPath.booleanValueOf(). I'm not sure that these are any more performant than selectNodes(), but may be clearer in your code.

best,
christian.

On Wednesday, November 19, 2003, at 03:59 PM, david wrote:


We need this for arbitrary nodes that are entered at runtime.



---------- Original Message ---------------------------------- From: Christian Niles <[EMAIL PROTECTED]> Date: Wed, 19 Nov 2003 15:39:12 -0500

To risk stating the obvious, you look for the node ;) In essence, you'd
just hand-code the XPath using the DOM4J methods such as
Element.element(String).


So, to search for the /html/head element,

Document doc = new SAXReader().read("http://www.dom4j.org/";);
Element htmlElem = doc.getRootElement();
Element headElem = htmlElem.element("head");
if (headElem != null) {
        // node exists
}

This is more efficient, but requires you to know the document structure
beforehand. Use XPath to either save yourself coding when performance
isn't an issue, or when you need to be able to search for an arbitrary
node.


christian.

On Wednesday, November 19, 2003, at 12:39 AM, David Thielen wrote:

 

----- Original Message -----
From: David Thielen
To: [EMAIL PROTECTED]
Sent: Sunday, November 16, 2003 2:59 PM
Subject: Re: [dom4j-user] [dom4j] - Does node exist

Actually this doesn't always work either. The best I have some up with
is:
 
    node.selectNodes(text).size() > 0
 
which I am assuming is not very efficient.
 
Is there a better way????
 
thanks - dave
 
 


----- Original Message -----
From: David Thielen
To: [EMAIL PROTECTED]
Sent: Sunday, November 16, 2003 1:45 PM
Subject: [dom4j-user] [dom4j] - Does node exist

Hi;
 
What is the most efficient way to determine if a node exists. The only
way I have founs so far is:
 
if (node.matches(text))
 
where text is the xpath to the node I am looking for and node is a
Node I start the search from.
 
thanks - dave








------------------------------------------------------- This SF.net email is sponsored by: SF.net Giveback Program. Does SourceForge.net help you be more productive? Does it help you create better code? SHARE THE LOVE, and help us help YOU! Click Here: http://sourceforge.net/donate/ _______________________________________________ dom4j-user mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to