Exemple #1
0
  /**
   * Parses content of an <code>InputSource</code> to create an XML <code>Element</code> object.
   *
   * @param source the input source that is supposed to contain XML to be parsed, not <code>null
   *     </code>.
   * @return the parsed result, not <code>null</code>.
   * @throws IOException if there is an I/O error.
   * @throws ParseException if the content is not considered to be valid XML.
   */
  private Element parse(InputSource source) throws IOException, ParseException {

    // TODO: Consider using an XMLReader instead of a SAXParser

    // Initialize our SAX event handler
    Handler handler = new Handler();

    try {
      // Let SAX parse the XML, using our handler
      SAXParserProvider.get().parse(source, handler);

    } catch (SAXException exception) {

      // TODO: Log: Parsing failed
      String exMessage = exception.getMessage();

      // Construct complete message
      String message = "Failed to parse XML";
      if (TextUtils.isEmpty(exMessage)) {
        message += '.';
      } else {
        message += ": " + exMessage;
      }

      // Throw exception with message, and register cause exception
      throw new ParseException(message, exception, exMessage);
    }

    Element element = handler.getElement();

    return element;
  }