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

xml validator problem

 
Howie Jiang
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.*;
import java.io.File;
import java.io.FileInputStream;
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();
dbf.setValidating(true);
dbf.setNamespaceAware(true);
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() );
}
});

db.parse(xmlf);
}
catch (Exception e) {
e.printStackTrace();
}


}

}
---------------------------------
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(validator.java:39)
-------------------------------------
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
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you referencing your Schema document from the XML document?
Here's an example from w3schools.com:
 
Balaji Loganathan
author and deputy
Bartender
Posts: 3150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.This tutorial may also help u further.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic