1
votes

Currently I am trying to get data (XML) from my MarkLogic server with the Java API.

Therefore I added namespaces:

NamespacesManager nsManager = client.newServerConfigManager()
  .newNamespacesManager();
nsManager.addPrefix("document",
  "http://test/dummy/master/doc");
...

After that I tried following:

DatabaseClient client = DatabaseClientFactory.newClient("IP_ADDRESS",
  PORT, user, password, Authentication.DIGEST);

SearchHandle handle = new SearchHandle();
QueryManager queryMgr = client.newQueryManager();

KeyValueQueryDefinition query = queryMgr.newKeyValueDefinition();
query.put(
  queryMgr.newElementLocator(new QName("doc:id")),
  "1439-1074");
SearchHandle resultsHandle = queryMgr.search(query, handle);
System.out.println("results: " + resultsHandle.getTotalResults());

// System.out.println("Matched "+resultsHandle.getTotalResults()+
// " documents with '"+query.getCriteria()+"'\n");

// iterate over the result documents
MatchDocumentSummary[] docSummaries = resultsHandle.getMatchResults();
System.out.println("Listing "+docSummaries.length+" documents:\n");

All works; I'm getting results but they don't contain the XML document (just URI to the document). Is it possible to get the XML result with this query or do I have to commit a second query like:

JSONDocumentManager docMgr = client.newJSONDocumentManager();
StringHandle doc = docMgr.read(uri, new StringHandle());
2

2 Answers

1
votes

You have to use a results handle that matches up with the results you want. Take a look at http://developer.marklogic.com/learn/java/processing-search-results and http://docs.marklogic.com/javadoc/client/index.html?com/marklogic/client/io/SearchHandle.html

Instead of using a SearchHandle, use a StringHandle. Here is the docs example:

// create a handle for the search results to be received as raw XML
StringHandle resultsHandle = new StringHandle();
// run the search
queryMgr.search(query, resultsHandle);
// dump the XML results to the console
System.out.println(resultsHandle);
1
votes

To get back the entire document instead of a snippet, specify raw transform results in your query options:

<transform-results apply="raw" />

Please see:

http://docs.marklogic.com/guide/search-dev/search-api#id_58295

If the documents are XML, you might want to use a built-in XML parsing handle such as DOMHandle, SAXHandle, or XMLStreamReaderHandle (or the techniques shown by the JDOMHandle or XOMHandle examples) instead of SearchHandle and extract the documents from the response payload.

If the documents are JSON, you might want to use the technique shown by the JacksonHandle example.