Class XMLDocumentParser


  • public class XMLDocumentParser
    extends Object
    This class contains methods to read and validate data from an XML DOM.
    • Constructor Detail

      • XMLDocumentParser

        public XMLDocumentParser()
    • Method Detail

      • parseElemValue

        public <T> T parseElemValue​(Element parent,
                                    String name,
                                    XMLValueParser<T> parser,
                                    T defaultVal)
                             throws ParseException
        Retrieves the string content of the specified child element and parses it using the specified parser. If the child element is not found, it returns the default value.
        Type Parameters:
        T - the value type
        Parameters:
        parent - the parent element
        name - the name of the child element
        parser - the parser
        defaultVal - the default value
        Returns:
        the parsed value or the default value
        Throws:
        ParseException - if the string content of the child element is invalid
        See Also:
        parseStringContent(Element)
      • parseAttrValue

        public <T> T parseAttrValue​(Element elem,
                                    String name,
                                    XMLValueParser<T> parser,
                                    T defaultVal)
                             throws ParseException
        Retrieves the value of the specified attribute and parses it using the specified parser. If the attribute is not found, it returns the default value.
        Type Parameters:
        T - the value type
        Parameters:
        elem - the element
        name - the name of the attribute
        parser - the parser
        defaultVal - the default value
        Returns:
        the parsed value or the default value
        Throws:
        ParseException - if the attribute value is invalid
      • parseRequiredElemValue

        public <T> T parseRequiredElemValue​(Element parent,
                                            String name,
                                            XMLValueParser<T> parser)
                                     throws ParseException
        Retrieves the string content of a required child element and parses it using the specified parser. If the child element is not found, it throws an exception.
        Type Parameters:
        T - the value type
        Parameters:
        parent - the parent element
        name - the name of the child element
        parser - the parser
        Returns:
        the parsed value
        Throws:
        ParseException - if the child element is not found or the string content of the child element is invalid
        See Also:
        parseStringContent(Element)
      • parseRequiredAttrValue

        public <T> T parseRequiredAttrValue​(Element elem,
                                            String name,
                                            XMLValueParser<T> parser)
                                     throws ParseException
        Retrieves the value of a required attribute and parses it using the specified parser. If the attribute is not found, it throws an exception.
        Type Parameters:
        T - the value type
        Parameters:
        elem - the element
        name - the name of the attribute
        parser - the parser
        Returns:
        the parsed value
        Throws:
        ParseException - if the attribute is not found or the attribute value is invalid
      • parseContent

        public <T> T parseContent​(Element elem,
                                  XMLValueParser<T> parser)
                           throws ParseException
        Retrieves the string content of the specified element and parses it using the specified parser.
        Type Parameters:
        T - the value type
        Parameters:
        elem - the element
        parser - the parser
        Returns:
        the parsed value
        Throws:
        ParseException - if the string content is invalid
        See Also:
        parseStringContent(Element)
      • parseStringContent

        public String parseStringContent​(Element elem)
        Parses the string content of the specified element. It will trim white space at the start and end. If the element contains other nodes than text nodes, then those nodes and any white space around them will be replaced by a single space.
        Parameters:
        elem - the element
        Returns:
        the string content
      • getChild

        public Element getChild​(Element parent,
                                String name)
        Returns the first child element with the specified name. If no such child exists, this method returns null.
        Parameters:
        parent - the parent element
        name - the name of the child element
        Returns:
        the child element or null