XML + XSD: Is it possible to get all possible Values for an Element?

Discussion in 'Java' started by Markus, Nov 22, 2005.

  1. Markus

    Markus Guest

    Here is the (very short) situation: :)
    I have a XML-file and a XSD-file and I'm using JDOM.

    Is there a way to get all possible values (attributes, sub-elements,
    text) for an Element from the DocumentTree?

    Kind regards

    Markus
    Markus, Nov 22, 2005
    #1
    1. Advertising

  2. Markus

    Markus Guest

    Here is the solution: :)

    m_SAXErrorHandler = new ErrorHandlerImpl();
    m_builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
    m_builder.setValidation(a_b_validate);
    m_builder.setErrorHandler(m_SAXErrorHandler);
    m_builder.setFeature("http://apache.org/xml/features/validation/schema",
    a_b_validate);
    m_document = m_builder.build(a_inputStreamXML);
    m_rootElement = m_document.getRootElement();

    If the document is not wellformed a JDOMException is throws and if
    errors against the XSD exist they a stored in m_SAXErrorHandler (a
    class which implements ErrorHandler).

    Example for ErrorHandlerImpl:
    public class SAXErrorHandler implements ErrorHandler {

    ArrayList m_warnings = new ArrayList();
    ArrayList m_errors = new ArrayList();
    ArrayList m_fatalErrors = new ArrayList();

    /**
    * @see org.xml.sax.ErrorHandler#warning(SAXParseException)
    */
    public void warning(SAXParseException arg0) throws SAXException {
    m_warnings.add(arg0);
    System.out.println("Warning: " + arg0.getLocalizedMessage());
    }

    /**
    * @see org.xml.sax.ErrorHandler#error(SAXParseException)
    */
    public void error(SAXParseException arg0) throws SAXException {
    m_errors.add(arg0);
    System.out.println("Error: " + arg0.getLocalizedMessage());
    }

    /**
    * @see org.xml.sax.ErrorHandler#fatalError(SAXParseException)
    */
    public void fatalError(SAXParseException arg0) throws SAXException {
    m_fatalErrors.add(arg0);
    System.out.println("FatalError: " + arg0.getLocalizedMessage());
    }

    public boolean hasWarnings() {
    return !this.m_warnings.isEmpty();
    }

    public boolean hasErrors() {
    return !this.m_errors.isEmpty();
    }

    public boolean hasFatalErrors() {
    return !this.m_fatalErrors.isEmpty();
    }

    public ArrayList getWarnings() {
    return this.m_warnings;
    }

    public ArrayList getErrors() {
    return this.m_errors;
    }

    public ArrayList getFatalErrors() {
    return this.m_fatalErrors;
    }
    }

    Greetings

    Markus
    Markus, Nov 22, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus
    Replies:
    1
    Views:
    1,079
    Markus
    Nov 22, 2005
  2. Peter Aberline

    xsd:any as a child of xsd:all

    Peter Aberline, Apr 5, 2004, in forum: XML
    Replies:
    0
    Views:
    771
    Peter Aberline
    Apr 5, 2004
  3. Matt
    Replies:
    3
    Views:
    37,199
    MKNDK
    Sep 11, 2008
  4. stiank81
    Replies:
    4
    Views:
    15,343
    stiank81
    Jun 26, 2005
  5. Rushi
    Replies:
    1
    Views:
    540
Loading...

Share This Page