public class XmlReader extends Object implements AutoCloseable
This class wraps the XMLStreamReader
class so that the methods don’t send checked exceptions, in order to
simplify its usage together with streams and lambdas.
Constructor and Description |
---|
XmlReader(File file)
Creates a reader that will read from the given file, using UTF-8 as the encoding.
|
XmlReader(InputStream in)
Creates an XML reader that will read from the given stream, using UTF-8 as the encoding.
|
XmlReader(Reader in)
Creates an XML reader that will read from the given reader.
|
XmlReader(Source source)
Creates an XML reader that will read from the given source.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the XML document and the underlying source.
|
boolean |
forward()
Jumps to the next start tag, end tag or end of document.
|
int |
getAttributeCount()
Returns the number of attributes of the current element.
|
String |
getAttributeLocalName(int index)
Returns the name of the attribute corresponding to the given index.
|
String |
getAttributeValue(int index)
Returns the value of the attribute corresponding to the given index.
|
int |
getEventType()
Returns the type of the current event.
|
String |
getLocalName()
Returns the name of the current element.
|
int |
next()
Gets the next parsing event.
|
boolean |
parseBoolean(String image) |
Date |
parseDate(String image) |
BigDecimal |
parseDecimal(String image) |
BigInteger |
parseInteger(String image) |
boolean |
readBoolean()
Reads a boolean value from the given XML reader, assuming that the cursor is positioned at the start
element that contains the value of the boolean.
|
List<Boolean> |
readBooleans()
Reads an list of booleans elements with same element name, assuming that the cursor is positioned at the start
element that contains the value of the boolean and ends when different element name found.
|
Date |
readDate()
Reads a date value from the given XML reader, assuming that the cursor is positioned at the start
element that contains the value of the date.
|
List<Date> |
readDates()
Reads an list of dates elements with same element name, assuming that the cursor is positioned at the start
element that contains the value of the date and ends when different element name found.
|
BigDecimal |
readDecimal()
Reads an decimal value from the given XML reader, assuming that the cursor is positioned at the start
element that contains the value of the decimal.
|
List<BigDecimal> |
readDecimals()
Reads an list of decimals elements with same element name, assuming that the cursor is positioned at the start
element that contains the value of the decimal and ends when different element name found.
|
BigInteger |
readInteger()
Reads an integer value from the given XML reader, assuming that the cursor is positioned at the start
element that contains the value of the integer.
|
List<BigInteger> |
readIntegers()
Reads an list of integers elements with same element name, assuming that the cursor is positioned at the start
element that contains the value of the integers and ends when different element name found.
|
String |
readString()
Reads an string value from the given XML reader, assuming that the cursor is positioned at the start
element that contains the value of the string.
|
List<String> |
readStrings()
Reads an list of string elements with same element name, assuming that the cursor is positioned at the start
element that contains the value of the string and ends when different element name found.
|
void |
skip()
Skips the current element, and all the inner elements.
|
public XmlReader(Source source)
Creates an XML reader that will read from the given source.
source
- the source where the document will be read frompublic XmlReader(InputStream in)
Creates an XML reader that will read from the given stream, using UTF-8 as the encoding.
in
- the stream where the document will be read frompublic XmlReader(Reader in)
Creates an XML reader that will read from the given reader.
in
- the reader where the document will be read frompublic XmlReader(File file)
Creates a reader that will read from the given file, using UTF-8 as the encoding.
file
- the file where the document will be writtenpublic int getAttributeCount()
Returns the number of attributes of the current element.
public String getAttributeLocalName(int index)
Returns the name of the attribute corresponding to the given index.
index
- the index of the attributepublic String getAttributeValue(int index)
Returns the value of the attribute corresponding to the given index.
index
- the index of the attributepublic int next()
Gets the next parsing event.
public String getLocalName()
Returns the name of the current element.
public int getEventType()
Returns the type of the current event.
public boolean forward()
Jumps to the next start tag, end tag or end of document. Returns true
if stopped at a start
tag, false
otherwise.
public void skip()
Skips the current element, and all the inner elements. The reader will be positioned at the event after the
the current one. For example, if the input text is <root><current>…​</current><next/></root>
and the
reader is positioned at the start of the current
element, then the method will skip all the content
of the current
element, and will leave the reader positioned at the start of the next
element.
public boolean readBoolean()
Reads a boolean value from the given XML reader, assuming that the cursor is positioned at the start element that contains the value of the boolean.
public BigInteger readInteger()
Reads an integer value from the given XML reader, assuming that the cursor is positioned at the start element that contains the value of the integer.
public BigDecimal readDecimal()
Reads an decimal value from the given XML reader, assuming that the cursor is positioned at the start element that contains the value of the decimal.
public String readString()
Reads an string value from the given XML reader, assuming that the cursor is positioned at the start element that contains the value of the string.
public Date readDate()
Reads a date value from the given XML reader, assuming that the cursor is positioned at the start element that contains the value of the date.
public List<Boolean> readBooleans()
Reads an list of booleans elements with same element name, assuming that the cursor is positioned at the start element that contains the value of the boolean and ends when different element name found.
public List<BigInteger> readIntegers()
Reads an list of integers elements with same element name, assuming that the cursor is positioned at the start element that contains the value of the integers and ends when different element name found.
public List<BigDecimal> readDecimals()
Reads an list of decimals elements with same element name, assuming that the cursor is positioned at the start element that contains the value of the decimal and ends when different element name found.
public List<Date> readDates()
Reads an list of dates elements with same element name, assuming that the cursor is positioned at the start element that contains the value of the date and ends when different element name found.
public List<String> readStrings()
Reads an list of string elements with same element name, assuming that the cursor is positioned at the start element that contains the value of the string and ends when different element name found.
public BigInteger parseInteger(String image)
public BigDecimal parseDecimal(String image)
public boolean parseBoolean(String image)
public void close()
Closes the XML document and the underlying source.
close
in interface AutoCloseable
Copyright © 2016. All rights reserved.