Validating XML using the Xerces DOM parser

Discussion in 'Java' started by Helen, Mar 2, 2004.

  1. Helen

    Helen Guest

    I have a web app that reads an XML file uploaded by a user, parses it
    and adds the information inside it to a database.

    To make sure that the users aren't putting invalid data into the
    database, I thought the easiest way to do this would be to write an
    XML schema to validate it.

    I want all documents uploaded to be validated by this particular
    schema. I'm only interested in processing documents that conform to
    this schema. I want the parsing to fail for any other file.

    I've checked my XML file and my schema by validating them using XML
    Spy. XML Spy has no problems applying the schema to the xml file and
    picking up any deliberate errors that I put into the file.

    I'm using the Xerces DOM parser (version 2.6.2) to read the XML. When
    I run the code with valid XML, it works fine, but if I include errors
    in the XML file it doesn't pick them up.

    DOMParser parser = new DOMParser();
    parser.setFeature("http://xml.org/sax/features/namespaces", false);
    parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
    "http://localhost:8080/assetDB/schema/assetList.xsd");
    Parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking",
    true);
    parser.parse(xmlSource);
    parser.setErrorHandler(this);
    Document assetDoc = parser.getDocument();

    What I really want to do is this:
    get XML file
    Validate xml file
    If valid
    process file
    else
    give helpful error messages
    end if

    Am I even going about this the right way?

    I appreciate any help (or even pointers to good java/xml resources)

    Helen
    Helen, Mar 2, 2004
    #1
    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. Jasmin Wason
    Replies:
    0
    Views:
    488
    Jasmin Wason
    Aug 1, 2003
  2. Daniel Ng

    xerces-C++:- validating an DOM

    Daniel Ng, Aug 4, 2003, in forum: XML
    Replies:
    0
    Views:
    434
    Daniel Ng
    Aug 4, 2003
  3. bugbear
    Replies:
    0
    Views:
    1,006
    bugbear
    Aug 28, 2003
  4. Yona
    Replies:
    0
    Views:
    550
  5. Kevin Partin
    Replies:
    1
    Views:
    1,373
    Martin Honnen
    Feb 13, 2004
Loading...

Share This Page