Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question abt validation

 
Howie Jiang
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Howie Jiang
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seems JAXP can only do DTD checking, but cannnot do schema checking. why? and how?
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic