Page 1 of 1

working with objects returned from evaluateXPath()

Posted: Fri Jun 27, 2014 8:45 pm
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?

Re: working with objects returned from evaluateXPath()

Posted: Mon Jun 30, 2014 10:27 am
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

Re: working with objects returned from evaluateXPath()

Posted: Tue Jul 01, 2014 1:02 am
by Hydrogen
Thanks!