This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Validating xml against xsd Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Validating xml against xsd" Watch "Validating xml against xsd" New topic

Validating xml against xsd

Rajendra Gangarde

Joined: Sep 10, 2004
Posts: 7
I am Validating xml file against xsd. xsd file is given by ibm
so I am using following code.But it is giving problem in xsd file itself.
But if try to validate the xml with same xsd by using online utility it works.It is giving following error.Does anyone faced the same problem?
Thanks in advance
DocumentBuilderFactory: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl
error: InvalidRegex: Pattern value 'first|last|[-#]?[0-9]+' is not a valid regular expression. The reported error was: ''-' is an invalid character range. Write '\-'.'.
error: cvc-complex-type.3.2.2: Attribute 'handle' is not allowed to appear in element 'portletinstance'.
Root node: #comment

Code is given below:


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class XMLValidator {

public static final void main(String[] args) {
String xmlFile = "test11.xml";

try {
XMLValidator XMLValidator = new XMLValidator(xmlFile);
catch (Exception e) {
System.out.println( e.getClass().getName() +": "+ e.getMessage() );

public XMLValidator(String xmlFile) throws ParserConfigurationException, SAXException, IOException {

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
System.out.println("DocumentBuilderFactory: "+ factory.getClass().getName());

factory.setAttribute("", "");

// Specify our own schema - this overrides the schemaLocation in the xml file
//factory.setAttribute("", "file:./test.xsd");

DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler( new SimpleErrorHandler() );

Document document = builder.parse(xmlFile);
Node rootNode = document.getFirstChild();
System.out.println("Root node: "+ rootNode.getNodeName() );
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
The error message was attempting to tell you that the "minus" character, e.g. - .... is a reserved character in regular expressions, apparently. You should escape it by prefixing it with the blackslash ... so, [\-#]? might work for you.

I agree. Here's the link:
subject: Validating xml against xsd
Similar Threads
Parse/Validate with DSIG schema issue
Read File from jar file
Throws IllegalArguementException when validating XML with schema
validating xml
Issue in validating vanila XML