Project

Profile

Help

SequenceIterator to String[]

Added by Anonymous over 18 years ago

Legacy ID: #3490275 Legacy Poster: Brian Bailey (bpbailey)

I have an XQuery that returns a sequence of nodes. I would like to serialize these nodes to a string array, with each node as its own array element. You have some sample code on your documentation, titled Invoking XQuery from Java; however, the code does not work: ... SequenceIterator books = exp.iterator(dynamicContext); Properties props = new Properties(); props.setProperty(OutputKeys.METHOD, "xml"); props.setProperty(OutputKeys.INDENT, "no"); int nr = 1; while (true) { NodeInfo book = books.next(); if (book==null) break; System.out.println("===== BOOK " + nr + " ====="); QueryResult.serialize(book, new StreamResult(System.out), props); } It doesn't work because books.next() returns an Item rather than a NodeInfo, and serialize wants a NodeInfo. I can cast the next()'s return value to a NodeInfo, though. This gets me close to where I want to be. Here's my code: ByteArrayOutputStream out = new ByteArrayOutputStream(); SequenceIterator iter = xqe.iterator(dynamicContext); NodeInfo item; while ((item = (NodeInfo)iter.next()) != null) { QueryResult.serialize(item, new StreamResult(out), props, config); // For now, just output to the console. Later I will add this to an array. String output = out.toString(); System.out.println(output); System.out.println("***************************"); } But given that there are three results, my output looks something like this: <MyItem>1</MyItem> *************************** <MyItem>1</MyItem><MyItem>2</MyItem> *************************** <MyItem>1</MyItem><MyItem>2</MyItem><MyItem>3</MyItem> *************************** As you can see, the iterator and serializer aren't playing nice together. I would like to have an array like so: String[] output; output[0] = <MyItem>1</MyItem> output[1] = <MyItem>2</MyItem> output[2] = <MyItem>3</MyItem> What am I doing wrong? Am I even close? Thanks for your help.


Replies (2)

RE: SequenceIterator to String[] - Added by Anonymous over 18 years ago

Legacy ID: #3490642 Legacy Poster: Michael Kay (mhkay)

> You have some sample code on your documentation, titled Invoking > XQuery from Java; > however, the code does not work: > > NodeInfo book = books.next(); > > It doesn't work because books.next() returns an Item rather > than a NodeInfo, and serialize wants a NodeInfo. Sorry about that error. I'll fix the example to include the cast: NodeInfo book = (NodeInfo)books.next() > Here's my code: > ... > > But given that there are three results, my output looks > something like this: > > <MyItem>1</MyItem> > *************************** > <MyItem>1</MyItem><MyItem>2</MyItem> > *************************** > <MyItem>1</MyItem><MyItem>2</MyItem><MyItem>3</MyItem> > *************************** > > > What am I doing wrong? Am I even close? > I think what's happening here is simply that each call to serialize() is appending to the existing ByteOutputStream rather than creating a new one. If you create the output stream within the loop, you should be OK. Michael Kay http://www.saxonica.com/

RE: SequenceIterator to String[] - Added by Anonymous over 18 years ago

Legacy ID: #3491435 Legacy Poster: Brian Bailey (bpbailey)

Thanks Michael. I ended up calling out.reset() within the loop rather than recreating the ByteOutputStream. Stupid error on my part...

    (1-2/2)

    Please register to reply