working with objects returned from evaluateXPath()

Post here questions and problems related to oXygen frameworks/document types.
Hydrogen
Posts: 15
Joined: Fri Jan 27, 2012 11:40 am

working with objects returned from evaluateXPath()

Post by Hydrogen »

evaluateXPath() returns an array of Object. Exactly what object depends on the XPath expression but when the result is a sequence of nodes I seem to get classes from org.apache.xerces.dom. I'd like to use the appropriate methods on those objects.

Code: Select all


import org.apache.xerces.dom.*;              // Compilte time error
...
WSXMLTextEditorPage xmlTep...
Object[] xpr = xmlTep.evaluateXPath(xp);
ws.showInformationMessage(xpr[0].getClass().getName()); // e.g. org.apache.xerces.dom.AttrImpl
String xprs = ((NodeImpl) xpr[0]).GetNodeValue(); // compile time error
Apparently evaluateXPath() returns objects that are not on the class path.

Do I need to add more to the class path?
Am I trying to do something evaluateXPath() was not intended to support?
Radu
Posts: 9055
Joined: Fri Jul 09, 2004 5:18 pm

Re: working with objects returned from evaluateXPath()

Post by Radu »

Hi,

The objects implement the DOM interface but some of them might not be the Xerces DOM implementations you are trying to cast them to.
You should try to cast the elements to the DOM interfaces like org.w3c.dom.Element or org.w3c.dom.Node or for example in your case to org.w3c.dom.Attr.
In this way, you do not need to worry about what DOM implementation they are (some of them might be special implementations of the DOM interfaces created by our code).

You should also check first that the received objects implement the proper interface like:

Code: Select all


if(xpr[0] instanceof org.w3c.dom.Attr){
String xprs = ((org.w3c.dom.Attr) xpr[0]).getValue();
}
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Hydrogen
Posts: 15
Joined: Fri Jan 27, 2012 11:40 am

Re: working with objects returned from evaluateXPath()

Post by Hydrogen »

Thanks!
Post Reply