This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring Boot in Practice and have Somnath Musib on-line!
See this thread for details.
Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

xml validator problem

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you referencing your Schema document from the XML document?
Here's an example from w3schools.com:
 
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
.This tutorial may also help u further.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic