Woodstox : Peek to Next Element

Overview

Woodstox provides a feature to peek into the next XML element from the current XML element. This is achieved by using
XMLEventReader.peek()
method.

This sample program demonstrates how to peek into the next XML element.

Create the employee.xml as shown below.

Note that there are three XML elements:

  1. start element ’employee’
  2. character content ‘some text content’
  3. end element ’employee’

Create TestPeek class as shown below.

Use
XMLEvent
to get the start element (see line 26-39 below). Print the contents of start element (see line 28 below).

Once the start element is obtained then use
XmlEventReader.peek()
to peek into the next element which is the character text(see line 31 below). Print the contents of peek element (see line 35 below).

Note that according JSR-173 specification,
XmlEventReader.peek()
method returns the next element but does not take it off the stream.

The output of the program demonstrating peek functionality using Woodstox is shown below.

Download Source Code for this Program

Leave a Reply

Your email address will not be published. Required fields are marked *