Vanessa,
I don't know of an easier way. I do it the same way. Maybe we
should write one to do it the other way.
Mark
Vanessa Williams wrote:
> I want to query a collection and then remove the documents found by the
> query. I have something that works, but I had to go about it in a strange
> way. I just want to confirm that there isn't a better way to do it. Here's
> what I would have expected to work:
>
> // spacedb is my Collecion and getQueryService is just a
> // convenience function
> ResourceSet resultSet = getQueryService(spacedb).query(xpath);
> ResourceIterator results = resultSet.getIterator();
> while (results.hasMoreResources()) {
> Resource res = results.nextResource();
> if (res instanceof XMLResource) {
>
> // do some stuff with it if you want,
> // then remove it...
>
> spacedb.removeResource(res);
>
> // this throws an exception!
>
> }
>
> Instead, I have to do something like this:
>
> ResourceSet resultSet = getQueryService(spacedb).query(xpath);
> ResourceIterator results = resultSet.getIterator();
> while (results.hasMoreResources()) {
> Resource res = results.nextResource();
> if (res instanceof XMLResource) {
>
> // do some stuff with it if you want,
>
> // find the key (getId() doesn't work, apparently
> // because Resources from ResourceSets are "anonymous"?)
> try {
> Node xmldoc = res.getContentAsDOM().getFirstChild();
> NamedNodeMap attrs = xmldoc.getAttributes();
> Node keyNode = attrs.getNamedItemNS(
> "http://xml.apache.org/xindice/Query",
> "key");
> key = keyNode.getNodeValue();
> }
> catch (Exception e) {
> //deal with any DOM exceptions
> }
>
> // now that we have the key, remove resource from collection
> try {
> Resource doc = spacedb.getResource(key);
> if (doc != null) {
> spacedb.removeResource(doc);
> }
> }
> catch (Exception e) {
> // etc.
> }
> }
> }
>
> Is this the easiest way? Or am I missing something fundamental. It seems to
> me it shouldn't be this hard, but it was the only way I could get it to
> work. Any enlightenment appreciated.
>
> Cheers,
>
> Vanessa
--
Mark J Stang
Architect
Cybershop Systems
begin:vcard
n:Stang;Mark
x-mozilla-html:TRUE
adr:;;;;;;
version:2.1
email;internet:[EMAIL PROTECTED]
fn:Mark Stang
end:vcard