I guess I'm in a posting mood today. I worked up a
quick class that wraps the the DOM node output from
getContentAsDOM method. The class simple takes a
ResourceSet in its construtor and will return an DOM
Document, DOM4J Document, or XML as a String. Feel
free to use it, and if folks like it enough I'll add a
few more features and put it on my website for
download.
-------------------------------------
package com.evanborysko.util.db.xindice;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.QName;
import org.dom4j.io.DOMReader;
import org.dom4j.io.DOMWriter;
import org.xmldb.api.base.ResourceIterator;
import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.XMLResource;
public class XindiceQueryResultWrapper {
private QName resultTag;
private ResourceSet resultSet;
private Document resultDocument;
public XindiceQueryResultWrapper(ResourceSet
resultSet) {
this.resultSet = resultSet;
}
public void buildResultSet() throws XMLDBException {
Document tmpDocument;
DocumentFactory df = DocumentFactory.getInstance();
resultDocument =
df.createDocument(df.createElement(resultTag));
ResourceIterator results =
this.resultSet.getIterator();
while (results.hasMoreResources()) {
XMLResource res =
(XMLResource)results.nextResource();
DOMReader xmlReader = new DOMReader();
org.w3c.dom.Node node =
res.getContentAsDOM();
tmpDocument =
xmlReader.read(((org.w3c.dom.Document)
res.getContentAsDOM()));
Element elm = tmpDocument.getRootElement();
resultDocument.getRootElement().add(elm);
}
}
/**
* Returns the resultTag.
* @return QName
*/
public QName getResultTag() {
return resultTag;
}
/**
* Sets the resultTag.
* @param resultTag The resultTag to set
*/
public void setResultTag(String resultTag) {
this.resultTag = new QName(resultTag);
}
/**
* Sets the resultTag.
* @param resultTag The resultTag to set
*/
public void setResultTag(QName resultTag) {
this.resultTag = resultTag;
}
/**
* Returns the resultDocument.
* @return org.dom4j.Document
*/
public Document getResultDocument() {
return resultDocument;
}
/**
* Returns the resultDocument.
* @return String
*/
public String getResultDocumentAsString() {
return resultDocument.asXML();
}
/**
* Returns the resultDocument.
* @return org.w3c.dom.Document
*/
public org.w3c.dom.Document
getResultDocumentAsW3CDOM() throws DocumentException {
return new DOMWriter().write(this.resultDocument);
}
/**
* Sets the resultSet.
* @param resultSet The resultSet to set
*/
public void setResultSet(ResourceSet resultSet) {
this.resultSet = resultSet;
}
}
--------------------------------
Cheers,
Evan