This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Validating xml against xsd

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:


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
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic