[jdom-interest] Help:How to make JDOM work with validation on-still doesn't work !

Nagulapalli, Srinivas Srinivas_Nagulapalli at CINFIN.com
Fri Jul 20 13:21:39 PDT 2001


Thanks Miguel,
 But I am still having the same problem.
 Here is cut down versions of my files-, and I am stilling pulling hair as
to what I am 
 missing! I am using JDOM-Beta7, JDK1.3. Whenever I turn on validation I get
the following
 error (whether or not I add xsi:noNamespaceSchemaLocation="conduct.xsd"):

 =======Error I get when I run "java cinfine.Conduct conduct.xml":========
org.xml.sax.SAXParseException: General Schema Error: Grammar with uri :
conduct.xsd , can not found. 	at
org.apache.xerces.framework.XMLParser.reportError(XMLParser.java:1008) 	at
org.apache.xerces.validators.common.XMLValidator.reportRecoverableXMLError(X
MLValidator.java:1250) 	at
org.apache.xerces.validators.common.XMLValidator.validateElementAndAttribute
s(XMLValidator.java:2497) 	at
org.apache.xerces.validators.common.XMLValidator.callStartElement(XMLValidat
or.java:818) 	at
org.apache.xerces.framework.XMLDocumentScanner.scanElement(XMLDocumentScanne
r.java:1852) 	at
org.apache.xerces.framework.XMLDocumentScanner$ContentDispatcher.dispatch(XM
LDocumentScanner.java:1000) 	at
org.apache.xerces.framework.XMLDocumentScanner.parseSome(XMLDocumentScanner.
java:380) 	at
org.apache.xerces.framework.XMLParser.parse(XMLParser.java:900) 	at
org.jdom.input.SAXBuilder.build(SAXBuilder.java:287) 	at
org.jdom.input.SAXBuilder.build(SAXBuilder.java:617) 	at
cinfine.Conduct.main(Conduct.java:59)
========================================================================

 Any help, insights will be greatly appreciated.
 THANKS!!!!!
 -Srini
 

=======Begin of Conduct.xml========================
<?xml version="1.0" encoding="UTF-8" ?>
<cdm:actions 	xmlns:cdm="conduct.xsd" 
                  xmlns:xsi="http://www.w3c.org/2000/XMLSchema-instance"   >

	<cdm:template name="newQuoteTemplate">
		<cdm:renderer section="header" type="jsp">documentHead.jsp
</cdm:renderer>
		<cdm:renderer section="agencyInfo" type="jsp">agencyInfo.jsp
</cdm:renderer>
		<cdm:renderer section="quoteType" type="jsp">quoteType.jsp
</cdm:renderer>
	</cdm:template>
</cdm:actions>=======End of Conduct.xml========================

=======Begin of Conduct.xsd========================
<xsd:schema targetNamespace="conduct.xsd"
xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"  
            xmlns:cdm="conduct.xsd" xmlns="conduct.xsd">	

	<xsd:complexType name="RendererType">
		<xsd:attribute name="section" type="xsd:string"
use="required"/>
		<xsd:attribute name="type" type="xsd:string"
use="required"/>
	</xsd:complexType>

	<xsd:complexType name="TemplateType">
		<xsd:sequence>
			<xsd:element name="renderer" type="cdm:RendererType"
minOccurs="0" 
                               maxOccurs="unbounded"/>
		</xsd:sequence>
		<xsd:attribute name="name" type="xsd:string"
use="required"/>
	</xsd:complexType>

	<xsd:complexType name="ActionsType">
		<xsd:sequence>
			<xsd:element name="template" type="cdm:TemplateType"
minOccurs="0" 
                       maxOccurs="unbounded"/>
		</xsd:sequence>
	</xsd:complexType>

	<xsd:element name="actions" type="cdm:ActionsType"/>
	<xsd:element name="template" type="cdm:TemplateType"/>
</xsd:schema>
=======End of Conduct.xsd========================

=======Begin of Conduct.java========================
package cinfine;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

/**
 * <p><code>Conduct</code> reads an XML conduct file and
 *   builds a JDOM <code>Document</code> using a SAX 2.0
 *   parser.
 * </p>
 */
public class Conduct {

    /**
     * <p>
     * This provides a static entry point for creating a JDOM
     *   <code>{@link Document}</code> object using a SAX 2.0
     *   parser (an <code>XMLReader</code> implementation).
     * </p>
     *
     * @param args <code>String[]</code>
     *        <ul>
     *         <li>First argument: filename of XML document to parse</li>
     *         <li>Second argument: optional name of SAX Driver class</li>
     *        </ul>
     */
    public static void main(String[] args) {
        if ((args.length != 1) && (args.length != 2)) {
            System.out.println(
                "Usage: java cinfine.Conduct " +
                "[XML document filename] ([SAX Driver Class])");
            return;
        }

        // Load filename and SAX driver class
        String filename = args[0];
        String saxDriverClass = null;
        if (args.length == 2) {
            saxDriverClass = args[1];
        }

        // Request document building with validation
        try {
            SAXBuilder builder = null;
            if (saxDriverClass == null) {
                builder = new SAXBuilder(true); //with validation- this line
does NOT work!
                //builder = new SAXBuilder(); //without validation - this
line works fine!
            } else {
                builder = new SAXBuilder(saxDriverClass);
            }

            File fileDoc = new File (filename);
            Document doc = builder.build(fileDoc.toURL());

            // Create an outputter
            XMLOutputter outputter = new XMLOutputter();
            outputter.output(doc, System.out);
        } catch (JDOMException e) {
            if (e.getCause() != null) {
                e.getCause().printStackTrace();
            } else {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}=======End of Conduct.java========================

-----Original Message-----
From: Miguel Angel Medina Lopez [mailto:mamedina at logic-factory.com]
Sent: Tuesday, June 26, 2001 3:07 AM
To: Miguel Angel Medina Lopez; Nagulapalli, Srinivas;
jdom-interest at jdom.org
Subject: Re: [jdom-interest] Help:How to make JDOM work with validation
on?


Hi:

 I validate xml document against schemas with JDOM. The next samples is with
beta6 and I don't know if work with beta 7.
After you create a instante of SAXBuilder with validate to true, you must
indicate the schema in your XML file and the validation is automatic when
you call method buid of SAXBuilder object. You indicate the schema URL with
the nexy line at top of your XML document.

<?xml version="1.0"?>
<ROOT xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
                     xsi:noNamespaceSchemaLocation="schema_url_or_file.xsd">

If you create the document with the JDOM API methods, you must write the
next lines to include this attribute to the document:

        root = new Element("ROOT");
        root.addAttribute(new
Attribute("noNamespaceSchemaLocation","schema_url_or_file.xsd",Namespace.get
Namespace("xsi","http://www.w3.org/1999/XMLSchema-instance")));

        doc = new Document(root);

I hope this help you

-------------------------------------------------------------
Miguel Ángel Medina López
Logic Factory: www.logic-factory.com
Granada - España





More information about the jdom-interest mailing list