[jdom-interest] DOMBuilder validation -api error in DOMBuilde r.java

philip.nelson at omniresources.com philip.nelson at omniresources.com
Thu Jun 15 07:44:46 PDT 2000


>  
>      DOMBuilder builder = new DOMBuilder(true);
>                               /*         ^^^^       */
>                               /* Turn on validation */
>      // start parsing...
>      DOMParser parser = new DOMParser();  // Xerces specific class
>      for (int i = 0; i < args.length; i++) {
>  
>        try {
>          // Read the entire document into memory
>          parser.parse(args[i]);
>  
>          org.w3c.dom.Document domDoc  = parser.getDocument();
>          org.jdom.Document    jdomDoc = builder.build(domDoc);

Interesting.  The DOMBuilder class has the flag for validation but never
uses it anywhere.   The reason it doesn't use is that the DOMBuilder doesn't
do any parsing; it's handed a DOM tree and the parsing has already happened.
It's the adapter that has to do the validation.  Here is slightly modified
code from the DOMBuilder sample to correctly turn validation on and off. 

DOMAdapter domadapter;
FileInputStream in = new FileInputStream(filename);
try {
	Class parserClass = Class.forName(domAdapterClass);
	domadapter = (DOMAdapter)parserClass.newInstance();
} catch (ClassNotFoundException e) {//stuff here}


// Build the JDOM Document - validating is off by default for this example
// because of the document root issue

boolean validate = true;
org.w3c.dom.Document w3cdoc = domadapter.getDocument((InputStream)in,
validate);
Document doc = builder.build(w3cdoc);



More information about the jdom-interest mailing list