[#784] Add Result.exportXML() to retrieve a DOM Document similar to that of .formatXML()

This commit is contained in:
Lukas Eder 2011-08-25 21:29:21 +00:00
parent e35c42e41f
commit 6e14abbdbf
3 changed files with 77 additions and 1 deletions

View File

@ -78,6 +78,7 @@ import java.util.concurrent.TimeUnit;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.apache.commons.io.FileUtils;
@ -3689,7 +3690,6 @@ public abstract class jOOQAbstractTest<
@Test
public void testFormatXML() throws Exception {
List<Field<?>> fields = TBook().getFields();
Result<B> books = create().selectFrom(TBook()).fetch();
String xml = books.formatXML();
InputStream is = new ByteArrayInputStream(xml.getBytes());
@ -3698,9 +3698,20 @@ public abstract class jOOQAbstractTest<
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(is);
testXML(doc, books);
}
@Test
public void testExportXML() throws Exception {
Result<B> books = create().selectFrom(TBook()).fetch();
testXML(books.exportXML(), books);
}
private void testXML(Document doc, Result<B> books) throws XPathExpressionException {
XPathFactory xpfactory = XPathFactory.newInstance();
XPath xp = xpfactory.newXPath();
List<Field<?>> fields = TBook().getFields();
assertEquals("1", xp.evaluate("count(/result)", doc));
assertEquals("1", xp.evaluate("count(/result/fields)", doc));
assertEquals("" + fields.size(),

View File

@ -44,6 +44,8 @@ import java.sql.Time;
import java.sql.Timestamp;
import java.util.List;
import org.w3c.dom.Document;
/**
* A wrapper for database results returned by <code>{@link SelectQuery}</code>
*
@ -1722,11 +1724,21 @@ public interface Result<R extends Record> extends FieldProvider, List<R>, Attach
/**
* Get this result formatted as XML
*
* @see #exportXML()
* @see <a
* href="http://www.jooq.org/xsd/jooq-export-1.6.2.xsd">http://www.jooq.org/xsd/jooq-export-1.6.2.xsd</a>
*/
String formatXML();
/**
* Get this result as XML
*
* @see #formatXML()
* @see <a
* href="http://www.jooq.org/xsd/jooq-export-1.6.2.xsd">http://www.jooq.org/xsd/jooq-export-1.6.2.xsd</a>
*/
Document exportXML();
/**
* Map resulting records onto a custom type.
* <p>

View File

@ -58,6 +58,10 @@ import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.jooq.ArrayRecord;
import org.jooq.Attachable;
import org.jooq.AttachableInternal;
@ -69,6 +73,8 @@ import org.jooq.RecordHandler;
import org.jooq.Result;
import org.jooq.Store;
import org.jooq.tools.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
* @author Lukas Eder
@ -1177,6 +1183,53 @@ class ResultImpl<R extends Record> implements Result<R>, AttachableInternal {
return sb.toString();
}
@Override
public final Document exportXML() {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element eResult = document.createElement("jooq-export:result");
eResult.setAttribute("xmlns:jooq-export", "http://www.jooq.org/xsd/jooq-export-1.6.2.xsd");
document.appendChild(eResult);
Element eFields = document.createElement("fields");
eResult.appendChild(eFields);
for (Field<?> field : getFields()) {
Element eField = document.createElement("field");
eField.setAttribute("name", field.getName());
eFields.appendChild(eField);
}
Element eRecords = document.createElement("records");
eResult.appendChild(eRecords);
for (Record record : this) {
Element eRecord = document.createElement("record");
eRecords.appendChild(eRecord);
for (Field<?> field : getFields()) {
Object value = record.getValue(field);
Element eValue = document.createElement("value");
eValue.setAttribute("field", field.getName());
eRecord.appendChild(eValue);
if (value != null) {
eValue.setTextContent(format0(value));
}
}
}
return document;
}
catch (ParserConfigurationException ignore) {
throw new RuntimeException(ignore);
}
}
private final String escapeXML(String string) {
return StringUtils.replaceEach(string,
new String[] { "\"", "'", "<", ">", "&" },