Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6826585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:09:54+00:00 2026-05-26T22:09:54+00:00

I am validating an in-memory DOM object using the javax.xml.validation.Validator class against an XSD

  • 0

I am validating an in-memory DOM object using the javax.xml.validation.Validator class against an XSD schema. I am getting a SAXParseException being thrown during the validation whenever there is some data corruption in the information I populate my DOM from.

An example error:

org.xml.SAXParseException: cvc-datatype-valid.1.2.1: ‘???”??[?????G?>???p~tn??~0?1]’ is not a valid valud for ‘hexBinary’.

What I am hoping is that there is a way to find the location of this error in my in-memory DOM and print out the offending element and its parent element. My current code is:

public void writeDocumentToFile(Document document) throws XMLWriteException {
  try {
    // Validate the document against the schema
    Validator validator = getSchema(xmlSchema).newValidator();
    validator.validate(new DOMSource(document));

    // Serialisation logic here.

  } catch(SAXException e) {
    throw new XMLWriteException(e); // This is being thrown
  } // Some other exceptions caught here.
}

private Schema getSchema(URL schema) throws SAXException {
  SchemaFactory schemaFactory = 
    SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

  // Some logic here to specify a ResourceResolver

  return schemaFactory.newSchema(schema);
}

I have looked into the Validator#setErrorHandler(ErrorHandler handler) method but the ErrorHandler interface only gives me exposure to a SAXParseException which only exposes the line number and column number of the error. Because I am using an in-memory DOM this returns -1 for both line and column number.

Is there a better way to do this? I don’t really want to have to manually validate the Strings before I add them to the DOM if the libraries provide me the function I’m looking for.

I’m using JDK 6 update 26 and JDK 6 update 7 depending on where this code is running.

EDIT: With this code added –

validator.setErrorHandler(new ErrorHandler() {
  @Override
  public void warning(SAXParseException exception) throws SAXException {
    printException(exception);
    throw exception;
  }

  @Override
  public void error(SAXParseException exception) throws SAXException {
    printException(exception);
    throw exception;
  }

  @Override
  public void fatalError(SAXParseException exception) throws SAXException {
    printException(exception);
    throw exception;
  }

  private void printException(SAXParseException exception) {
    System.out.println("exception.getPublicId() = " + exception.getPublicId());
    System.out.println("exception.getSystemId() = " + exception.getSystemId());
    System.out.println("exception.getColumnNumber() = " + exception.getColumnNumber());
    System.out.println("exception.getLineNumber() = " + exception.getLineNumber());
  }
});

I get the output:

exception.getPublicId() = null
exception.getSystemId() = null
exception.getColumnNumber() = -1
exception.getLineNumber() = -1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:09:55+00:00Added an answer on May 26, 2026 at 10:09 pm

    If you are using Xerces (the Sun JDK default), you can get the element that failed validation through the http://apache.org/xml/properties/dom/current-element-node property:

    ...
    catch (SAXParseException e)
    {
        Element curElement = (Element)validator.getProperty("http://apache.org/xml/properties/dom/current-element-node");
    
        System.out.println("Validation error: " + e.getMessage());
        System.out.println("Element: " + curElement);
    }   
    

    Example:

    String xml = "<root xmlns=\"http://www.myschema.org\">\n" +
                 "<text>This is text</text>\n" +
                 "<number>32</number>\n" +
                 "<number>abc</number>\n" +
                 "</root>";
    
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    Document doc = dbf.newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
    Schema schema = getSchema(getClass().getResource("myschema.xsd"));
    
    Validator validator = schema.newValidator();
    try
    {
        validator.validate(new DOMSource(doc));
    }
    catch (SAXParseException e)
    {
        Element curElement = (Element)validator.getProperty("http://apache.org/xml/properties/dom/current-element-node");
    
        System.out.println("Validation error: " + e.getMessage());
        System.out.println(curElement.getLocalName() + ": " + curElement.getTextContent());
    
        //Use curElement.getParentNode() or whatever you need here
    }         
    

    If you need to get line/column numbers from the DOM, this answer has a solution to that problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Java, I can validate an XML document against an XSD schema using javax.xml.validation.Validator,
While validating my CSS on http://jigsaw.w3.org/css-validator/ I am getting following errors: 1.Property zoom doesn't
I am having trouble validating my xml schema. I get these errors on the
I need to validate a XML file against a schema. The XML file is
I am using data annotations to validation my class properties. A requirement has emerged
We are validating XML files and depending on the result of the validation we
I am trying to build my first XML schema validator as a reusable component
I am validating a xml with xsd in a SSIS package. i have a
I am validating a form with the jQuery validation plugin (using the plugin is
I am currently validating a client's HTML Source and I am getting a lot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.