wood burning stoves 2.0
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
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:

package com.hsbc.bde.deployment;

import java.io.IOException;
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("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");

// Specify our own schema - this overrides the schemaLocation in the xml file
//factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", "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: http://aspose.com/file-tools
subject: Validating xml against xsd
It's not a secret anymore!