This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes xml validator problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "xml validator problem" Watch "xml validator problem" New topic

xml validator problem

Howie Jiang

Joined: Aug 25, 2003
Posts: 26
Hi, I am doing a xml validation now. below is my code:
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
import javax.xml.parsers.*;
public class validator {

public static void main(String[] args) {
try {
String xmlpath = "D:\\documents\\abc.xml";
File xmlf = new File(xmlpath);

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
db.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) {
String schemapath = "D:\\documents\\cde.xsd";
File schemaf = new File(schemapath);
try {
FileInputStream fis = new FileInputStream(schemaf);
return new InputSource(fis);
catch (Exception e) {return null;}
db.setErrorHandler(new DefaultHandler() {
public void error(SAXParseException e) throws SAXParseException { throw e; }
public void fatalError(SAXParseException e) throws SAXParseException { throw e; }
public void warning(SAXParseException e) throws SAXParseException {
System.out.println( e.toString() );

catch (Exception e) {


the xsd is correct. And the xml is valid i think. but when i run this program, it give me the following error.
org.xml.sax.SAXParseException: Valid documents must have a <!DOCTYPE declaration
org.xml.sax.SAXParseException: Element type "CollaborationProtocolProfile" is no
t declared.
at org.apache.crimson.parser.Parser2.error(Unknown Source)
at org.apache.crimson.parser.Parser2.maybeElement(Unknown Source)
at org.apache.crimson.parser.Parser2.parseInternal(Unknown Source)
at org.apache.crimson.parser.Parser2.parse(Unknown Source)
at org.apache.crimson.parser.XMLReaderImpl.parse(Unknown Source)
at org.apache.crimson.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at validator.main(
for the first error, i don't know what it means.
for the second error, i have declared it in the schema, but why still said not defined?
can anybody shed some lite...
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
Are you referencing your Schema document from the XML document?
Here's an example from

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Balaji Loganathan
author and deputy

Joined: Jul 13, 2001
Posts: 3150
.This tutorial may also help u further.

Spritle Software Blogs
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: xml validator problem
It's not a secret anymore!