Class XmlReader

  • All Implemented Interfaces:
    AutoCloseable

    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 Summary

      Constructors 
      Constructor 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.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method 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.
      String getAttributeValue​(String name)
      Returns the value of the attribute corresponding to the given name.
      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)  
      Object read()
      Reads one object, determining the reader method to use based on the tag name of the first element.
      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 list element that contains the list of values with the string and ends when different element name found.
      static void register​(String tag, Method reader)
      Registers a read method.
      void skip()
      Skips the current element, and all the inner elements.
    • Constructor Detail

      • XmlReader

        public XmlReader​(Source source)
        Creates an XML reader that will read from the given source.
        Parameters:
        source - the source where the document will be read from
      • XmlReader

        public XmlReader​(InputStream in)
        Creates an XML reader that will read from the given stream, using UTF-8 as the encoding.
        Parameters:
        in - the stream where the document will be read from
      • XmlReader

        public XmlReader​(Reader in)
        Creates an XML reader that will read from the given reader.
        Parameters:
        in - the reader where the document will be read from
      • XmlReader

        public XmlReader​(File file)
        Creates a reader that will read from the given file, using UTF-8 as the encoding.
        Parameters:
        file - the file where the document will be written
    • Method Detail

      • getAttributeCount

        public int getAttributeCount()
        Returns the number of attributes of the current element.
      • getAttributeLocalName

        public String getAttributeLocalName​(int index)
        Returns the name of the attribute corresponding to the given index.
        Parameters:
        index - the index of the attribute
      • getAttributeValue

        public String getAttributeValue​(int index)
        Returns the value of the attribute corresponding to the given index.
        Parameters:
        index - the index of the attribute
      • getAttributeValue

        public String getAttributeValue​(String name)
        Returns the value of the attribute corresponding to the given name.
        Parameters:
        name - the name of the attribute
      • next

        public int next()
        Gets the next parsing event.
      • getLocalName

        public String getLocalName()
        Returns the name of the current element.
      • getEventType

        public int getEventType()
        Returns the type of the current event.
      • forward

        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.
      • skip

        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.
      • readBoolean

        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.
      • readInteger

        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.
      • readDecimal

        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.
      • readString

        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.
      • readDate

        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.
      • readBooleans

        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.
      • readIntegers

        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.
      • readDecimals

        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.
      • readDates

        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.
      • readStrings

        public List<String> readStrings()
        Reads an list of string elements with same element name, assuming that the cursor is positioned at the list element that contains the list of values with the string and ends when different element name found.
      • parseDate

        public Date parseDate​(String image)
      • parseBoolean

        public boolean parseBoolean​(String image)
      • close

        public void close()
        Closes the XML document and the underlying source.
        Specified by:
        close in interface AutoCloseable
      • register

        public static void register​(String tag,
                                    Method reader)
        Registers a read method.
        Parameters:
        tag - The tag name.
        reader - The reference to the method that reads the object corresponding to the `tag`.
      • read

        public Object read()
        Reads one object, determining the reader method to use based on the tag name of the first element. For example, if the first tag name is vm then it will create a Vm object, if it the tag is vms it will create an array of Vm objects, so on.