Figuring out errors during XML validation

P

PBR

How can I capture the Element name, and generate meaningful error
messages, when a validation error occurrs.I am using java APIs to
validate (javax.xml.validation)
 
S

steve_marjoribanks

You can use methods such as exception.getMessage(),
exception.getLineNumber etc.to get information about an exception.

I'd recommend using the ErrorHandler interface if you're using an
XMLReader.
 
P

PBR

Steve Thanks for ur reply.
will i be able to get the element name where validation error had
occured if i use exception.getMessage(),exception.getLineNumber..?can u
give an example? (I am using ErrorHandler interface)
 
S

steve_marjoribanks

Um, yeah I'm fairly certain it gives a validation error message which
includes the element name, I'm sorry I don't have any examples to hand
but try something along the lines of:

class ParserErrors implements ErrorHandler
{
public void warning (SAXParseException e)
{
System.out.println("Warning!, Line: " + e.getLineNumber() +
"\nMessage: " + e.getMessage);
}

public void error (SAXParseException e)
{
etc....
}

public void fatalError (SAXParseException e)
{
etc...
}
}


and just try validating an invalid XML file and see what it gives you.
There are other methods available as well such as getColumnNumber(),
just take a look at the API. Use can use the results of these methods
in any way you want.

Steve
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top