/* Print the book titles with Selection, the java counterpart of
   XMQSelection in the C api. The xpath is evaluated once, when the
   selection is created, and the nodes are then taken one at a time in
   a for loop. No code of the caller ever runs inside the library, so
   a thrown exception cannot leak the selection: there is nothing to
   clean up.

   The document is created and parsed from an xmq buffer in one call
   with of. */

import org.libxmq.NotFoundException;
import org.libxmq.ParseException;
import org.libxmq.Query;
import org.libxmq.Selection;
import org.libxmq.XMQ;
import org.libxmq.XMQNode;

public class SelectionIterator
{
    public static void main(String[] args) throws ParseException, NotFoundException
    {
        Query library = new Query(XMQ.of("library{ book{ title=Dune } book{} book{ title=Emma } book{ title=Ulysses } book{ title=Neuromancer }}"));

        Selection titles = library.select("//title"); // Evaluated here, selection stores result.

        for (XMQNode title : titles)
        {
            System.out.println(title.textContent());
        }
    }
}