uxparser
Class XParser

java.lang.Object
  extended by uxparser.XParser

public class XParser
extends java.lang.Object

XParser is a very simple lightweight XML parser. It may be used as a pull parser by iterating through the element and text sections of an XML stream or it may be used to read an entire XML tree into memory as XElems.

XParser works in conjunction with XWriter to support plain text or PKZIP documents. This check happens automatically by sniffing the first few bytes of the input stream. If a zip file is detected, the first zip entry is parsed. Note that when reading from a zip file, no guarantee is made where the stream is positioned once the XML has been read.


Field Summary
static int ELEM_END
          Indicates parser currently on element end.
static int ELEM_START
          Indicates parser currently on element start.
static int EOF
          Indicates end of file (or input stream)
static int TEXT
          Indicates parser currently at character data.
 
Constructor Summary
protected XParser(java.io.InputStream in)
          Private constructor.
 
Method Summary
 void close()
          Close the underlying input stream.
 int column()
          Get current column number.
 int depth()
          Get the depth of the current element with the document root being a depth of one.
 XElem elem()
          Get the current element if type() is ELEM_START or ELEM_END.
 XElem elem(int depth)
          Get the at the current depth.
 java.lang.String getEncoding()
          Get the character encoding of the underlying input stream.
 boolean isZipped()
          Return if the stream was zipped.
 int line()
          Get current line number.
static XParser make(java.io.File file)
          Make an XParser to parse the specified file.
static XParser make(java.io.InputStream in)
          Make an XParser to parse XML from the specified input stream.
static XParser make(java.lang.String xml)
          Make an XParser to parse an ASCII string.
 int next()
          Advance the parser to the next node and return the node type.
 XElem parse()
          Convenience for parse(true).
 XElem parse(boolean close)
          Parse the entire next element into memory as a tree of XElems and optionally close the underlying input stream.
 XElem parseCurrent()
          Convenience for parseCurrent(false).
 XElem parseCurrent(boolean close)
          Parse the entire current element into memory as a tree of XElems and optionally close the underlying input stream.
 void skip()
          Convenience for skip(depth()).
 void skip(int toDepth)
          Skip parses all the content until reaching the end tag of the specified depth.
 XText text()
          If the current type is TEXT return the XText instance used to store the character data.
 int type()
          Get the current node type constant which is always the result of the last call to next().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EOF

public static final int EOF
Indicates end of file (or input stream)

See Also:
Constant Field Values

ELEM_START

public static final int ELEM_START
Indicates parser currently on element start.

See Also:
Constant Field Values

ELEM_END

public static final int ELEM_END
Indicates parser currently on element end.

See Also:
Constant Field Values

TEXT

public static final int TEXT
Indicates parser currently at character data.

See Also:
Constant Field Values
Constructor Detail

XParser

protected XParser(java.io.InputStream in)
           throws java.io.IOException
Private constructor.

Throws:
java.io.IOException
Method Detail

make

public static XParser make(java.io.File file)
                    throws java.lang.Exception
Make an XParser to parse the specified file.

Throws:
java.lang.Exception

make

public static XParser make(java.lang.String xml)
                    throws java.lang.Exception
Make an XParser to parse an ASCII string.

Throws:
java.lang.Exception

make

public static XParser make(java.io.InputStream in)
                    throws java.lang.Exception
Make an XParser to parse XML from the specified input stream.

Throws:
java.lang.Exception

getEncoding

public java.lang.String getEncoding()
                             throws java.io.IOException
Get the character encoding of the underlying input stream.

Throws:
java.io.IOException

isZipped

public boolean isZipped()
                 throws java.io.IOException
Return if the stream was zipped.

Throws:
java.io.IOException

parse

public final XElem parse()
                  throws java.lang.Exception
Convenience for parse(true).

Throws:
java.lang.Exception

parse

public final XElem parse(boolean close)
                  throws java.lang.Exception
Parse the entire next element into memory as a tree of XElems and optionally close the underlying input stream.

Throws:
java.lang.Exception

parseCurrent

public final XElem parseCurrent()
                         throws java.lang.Exception
Convenience for parseCurrent(false).

Throws:
java.lang.Exception

parseCurrent

public final XElem parseCurrent(boolean close)
                         throws java.lang.Exception
Parse the entire current element into memory as a tree of XElems and optionally close the underlying input stream.

Throws:
java.lang.Exception

next

public final int next()
               throws java.lang.Exception
Advance the parser to the next node and return the node type. Return the current node type: ELEM_START, ELEM_END, or TEXT. If no more data to parse then return EOF.

Throws:
java.lang.Exception

skip

public void skip()
          throws java.lang.Exception
Convenience for skip(depth()).

Throws:
java.lang.Exception

skip

public void skip(int toDepth)
          throws java.lang.Exception
Skip parses all the content until reaching the end tag of the specified depth. When this method returns, the next call to next() will return the element or text immediately following the end tag.

Throws:
java.lang.Exception

type

public final int type()
Get the current node type constant which is always the result of the last call to next(). This constant may be ELEM_START, ELEM_END, TEXT, or EOF.


depth

public final int depth()
Get the depth of the current element with the document root being a depth of one. A depth of 0 indicates a position before or after the root element.


elem

public final XElem elem()
Get the current element if type() is ELEM_START or ELEM_END. If type() is TEXT then this is the parent element of the current character data. After ELEM_END this XElem instance is no longer valid and will be reused for further processing. If depth is zero return null.


elem

public final XElem elem(int depth)
Get the at the current depth. Depth must be between 0 and depth() inclusively. Calling elem(0) will return the root element and elem(depth()-1) returns the current element. If depth is invalid, return null.


text

public final XText text()
If the current type is TEXT return the XText instance used to store the character data. After a call to next() this XText instance is no longer valid and will be reused for further processing. If the current type is not TEXT then return null.


line

public final int line()
Get current line number.


column

public final int column()
Get current column number.


close

public final void close()
Close the underlying input stream.