File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes question abt validation 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 "question abt validation" Watch "question abt validation" New topic
Author

question abt validation

Howie Jiang
Greenhorn

Joined: Aug 25, 2003
Posts: 26
I read this from ibm developers work website. Have some questions here.
-------------------------------------------
Validation in JAXP:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import org.w3c.dom.Document;
public class StructureTest {

public static void main (String args[]) {
File docFile = new File("memory.xml");

try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);

DocumentBuilder db = dbf.newDocumentBuilder();
ErrorChecker errors = new ErrorChecker();
db.setErrorHandler(errors);

Document doc = db.parse(docFile);
} catch (Exception e) {
System.out.print("Parsing problem.");
}
}
}
Question: Although we reference the xml schema or DTD from xml document, how can the above java program know where the xml schema or DTD is? I think the above program can only check whether the xml document is well-formatted. Can it also do validation for me? If cannot, anybody can share with me how can do it with JAXP?
----------------------------------------------------
Validation in Xerces Java
import org.apache.xerces.parsers.DOMParser;
import java.io.File;
import org.w3c.dom.Document;
public class SchemaTest {
public static void main (String args[]) {
File docFile = new File("memory.xml");

try {
DOMParser parser = new DOMParser();
parser.setFeature("http://xml.org/sax/features/validation", true);

parser.setProperty(
"http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
"memory.xsd");

ErrorChecker errors = new ErrorChecker();
parser.setErrorHandler(errors);
parser.parse("memory.xml");

} catch (Exception e) {
System.out.print("Problem parsing the file.");
}
}
}
Question: Since we set "memory.xsd" here as property of the parser, is it means parser will do validation against this xsd when parsing the xml document? Is it that we must put memory.xsd and memory.xml under the same directory with this java program?
---------------------------------------
Thanks for your answer very very much!! ;-)
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
I think you should also add the following property:

...which tells the parser to validate against the specified schema (instead of trying to validate against a DTD). Drop us a note whether it worked or not.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Howie Jiang
Greenhorn

Joined: Aug 25, 2003
Posts: 26
seems JAXP can only do DTD checking, but cannnot do schema checking. why? and how?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
JAXP is only a set of interfaces for abstracting away the actual parser implementation and doesn't "prevent" the parser from implementing schema validation. Besides, Xerces does support schema validation as is documented in the following resources:
http://www-106.ibm.com/developerworks/edu/x-dw-xxschema-i.html
http://xml.apache.org/xerces-j/features.html
Ofcourse, if you're using an older version of Xerces (older than 1.4.4?), it might not yet support schema validation...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question abt validation