• Post Reply Bookmark Topic Watch Topic
  • New Topic

Validating xml against xsd  RSS feed

Rajendra Gangarde
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!