You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by Steven Traut <st...@bea.com> on 2005/05/11 22:22:24 UTC
Cast query return value to xbean type?
Is it a mistake to expect to be able to cast the result from a cursor.getObject call to a type generated from schema?
The Java code below receives an XML document that conforms to my schema. When it uses a query to get certain elements of that document, the getObject return value representing one of those elements can't be cast to the matching type generated from my schema. Instead, I get a ClassCastException. The XML represented by the return value appears to match my schema just fine.
I can send the XML and schema if need be.
final static String m_namespaceDeclaration =
"declare namespace xq='http://xmlbeans.apache.org/samples/xquery/employees';";
public boolean selectEmpsByStateCursor(XmlObject empDoc)
{
boolean hasResults = false;
// A cursor instance to query with.
XmlCursor empCursor = empDoc.newCursor();
empCursor.toNextToken();
// Get the <employee> elements with <state> elements whose
// value is "WA".
String queryExpression =
"for $e in $this/xq:employees/xq:employee " +
"let $s := $e/xq:address/xq:state " +
"where $s = 'WA' " +
"return $e";
// Execute the query. Results, if any, will be available at
// the position of the resultCursor.
XmlCursor resultCursor =
empCursor.execQuery(m_namespaceDeclaration + queryExpression);
resultCursor.toFirstContentToken();
// Use the cursor to loop through the results, printing each sibling
// <employee> element returned by the query.
do
{
int i = 0;
if (resultCursor.currentTokenType() == XmlCursor.TokenType.START)
{
hasResults = true;
// EXCEPTION ON THIS LINE.
EmployeeType resultXml = (EmployeeType)resultCursor.getObject();
System.out.println("Result " + i + ":\n" + resultXml.toString() + "\n");
i++;
}
} while (resultCursor.toNextSibling());
return hasResults;
}
Thanks,
Steve
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org