I have included a piece of code that should give you an idea of what to do. I have renamed and cut out some of it to sanitize it for any IP.

I setup the entity resolver to look for the system id that you would have seen if you put in the XML instance document the schema information. This is how I do my XSD schema validation. BTW, I could only use Xerces to properly validate my schema, others just did not work correctly.

Hope that helps,
Dave

================================= SNIP ================================
public class XXX {
//...
static class MyEntityResolver implements EntityResolver {
InputStream in = null;
public MyEntityResolver() {
in = getClass().getResourceAsStream("my.xsd"
);
if (in==null) {
try{
in = new FileInputStream("xml/my.xsd");
}
catch(Exception e) {}
}
}
public InputSource resolveEntity(String publicId, String systemId)


    {
     if ( systemId.indexOf( "my.xsd" ) > 0 ) {
         return new InputSource( in );
     }
     return null;
    }
   };

private EntityResolver resolver = null;

void loadFromStream(Reader myStream) throws Exception {
SAXReader reader=null;
org.dom4j.util.XMLErrorHandler errorHandler=null;
try {
reader = fetchSAXReader();
reader.setValidation(validate);
if (validate) {
// specify the schema to use
reader.setFeature
("http://xml.org/sax/features/validation";, true);
try {
reader.setFeature ("http://apache.org/xml/features/validation/schema",true);
}
catch(Exception e) {
e.printStackTrace();
}


       // add an error handler which turns any errors into XML
       errorHandler = new org.dom4j.util.XMLErrorHandler();
       reader.setErrorHandler(errorHandler);

       if (resolver==null) {
         resolver = new MyEntityResolver();
       }
       reader.setEntityResolver(resolver);
      }
      Document tinxDoc = reader.read(tinxStream);
      myStream.close();

      if (validate) {
        Element errors = errorHandler.getErrors();
        if (errors.hasContent()) {
          System.out.println(" Validation Error: " + errors.asXML());
          String msg = "Document ";
          if (localName!=null) {
                  msg+=uri;
          }
          msg+=" is not valid! reason="+errors.asXML();
                throw new Exception(msg);
          }
        }

      } catch (Exception ex) {
        ex.printStackTrace();
        throw new Exception( "Failed trying to load Document!");
      }
    }
//...
}

================================= SNIP ================================


Naveen Murthy wrote:
Hi Dave,

I did what u said but still i get the same error..

Actually Im trying to validate against a Schema...but
it seems to be looking for DOCTYPE....I thought DOCTYPE
is only required for validating against dtds...
Am i right?

I put the following in my xml
<!DOCTYPE eventNotificationMsg SYSTEM "EventNotification_06232003.xsd">

then the DOCtype error goes away, but i get another error..
     [java]   <fatalError column="3" line="1">The markup declarations
contained
or pointed to by the document type declaration must be
well-formed.</fatalError>

It seems to be looking for a DTD and not a schema...
How do I tell the parser that I want to do a Schema validation (not DTD
validataion)??


Naveen




-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf Of David D.
Lucas
Sent: Thursday, June 26, 2003 11:44 PM
To: Naveen Murthy
Cc: [EMAIL PROTECTED]
Subject: Re: [dom4j-user] Error- validation using xml schema with dom4j


You xml instance needs to define what schema it is using. Apparently there is nothing handling the "null" schema.

Try something like:


<eventNotificationMsg xmlns="http://www.xxx.yyy.com"; xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"; xsi:schemaLocation = "http://www.xxx.yyy.com"; http://www.xxx.yyy.com/enm.xsd";>

...
</eventNotificationMsg>

Best wishes,
Dave


Naveen Murthy wrote:


Hello,

Im using dom4j 1.4 and im trying to validate my xml against a schema.
I tried using the SAXValidatorDemo.java (shipped along with dom4j) to test
the validation. I get the following error from the errorhandler

<errors>
 <error column="23" line="2">Document root element

"eventNotificationMsg",


must
match DOCTYPE root "null".</error>
 <error column="23" line="2">Document is invalid: no grammar

found.</error>


</errors>

Im using xml schemas, not dtds, so i didnot include the DOCTYPE for the

xml.


Any help is appreciated, Thank you.


My xml is as follows:




<eventNotificationMsg><WorkAGR><CENTER>NE1NS1000DI</CENTER><CUST_NAME>MAYNAR


D N


BURT</CUST_NAME><ESTIMATED_TIME>00:30</ESTIMATED_TIME><LOCATION>PTLDMEFO</LO


CATION><ORDER>MEAR306188</ORDER><PRIORITY>29</PRIORITY><REPORT1>LETS SEE

DO


&amp; DI | CH N


0</REPORT1><STATUS>T</STATUS><TRACKING>2077739865</TRACKING><TRICK1>N1</TRIC


K1><TRICK2>N2</TRICK2><WORK_CODE>ND</WORK_CODE><WORK_ID>175ND0007</WORK_ID><

WORK_TYPE>NDSTA</WORK_TYPE><ENTRY_DATE_TIME>2003-06-24T08:53:00</ENTRY_DATE_

TIME><DUE_DATE_TIME>2003-06-25T16:10:00</DUE_DATE_TIME><STATUS_DATE_TIME>200

3-06-24T08:53:00</STATUS_DATE_TIME><REGION>NE</REGION></WorkAGR></eventNotif

icationMsg>


my schema is as follows:


<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema";>
        <xs:element name="eventNotificationMsg" type="EventNotification">
                <xs:annotation>
                        <xs:documentation>Event Notification </xs:documentation>
                </xs:annotation>
        </xs:element>
        <xs:complexType name="EventNotification">
                <xs:all>
                        <xs:element name="WorkAGR" type="WorkAgrType"/>
                </xs:all>
        </xs:complexType>
        <xs:complexType name="WorkAgrType">
                <xs:all>
                        <xs:element name="REGION" type="xs:string" minOccurs="0"/>
                        <xs:element name="CENTER" type="xs:string" minOccurs="0"/>
                        <xs:element name="LOCATION" type="xs:string" minOccurs="0"/>
                        <xs:element name="WORK_ID" type="xs:string" minOccurs="0"/>
                        <xs:element name="WORK_CODE" type="xs:string" minOccurs="0"/>
                        <xs:element name="ENTRY_DATE_TIME" type="xs:dateTime" 
minOccurs="0"/>
                        <xs:element name="DUE_DATE_TIME" type="xs:dateTime" 
minOccurs="0"/>
                        <xs:element name="STATUS" type="xs:string" minOccurs="0"/>
                        <xs:element name="PRIORITY" type="xs:int" minOccurs="0"/>
                        <xs:element name="TRACKING" type="xs:string" minOccurs="0"/>
                        <xs:element name="ORDER" type="xs:string" minOccurs="0"/>
                        <xs:element name="CKT_ID" type="xs:string" minOccurs="0"/>
                        <xs:element name="CAC" type="xs:string" minOccurs="0"/>
                        <xs:element name="WORK_TYPE" type="xs:string" minOccurs="0"/>
                        <xs:element name="LOADED_DATE_TIME" type="xs:dateTime" 
minOccurs="0"/>
                        <xs:element name="TOUR" type="xs:string" minOccurs="0"/>
                        <xs:element name="ALIAS" type="xs:string" minOccurs="0"/>
                        <xs:element name="TRICK1" type="xs:string" minOccurs="0"/>

                        <xs:element name="TRICK2" type="xs:string" minOccurs="0"/>
                        <xs:element name="TRICK3" type="xs:string" minOccurs="0"/>
                        <xs:element name="TRICK4" type="xs:string" minOccurs="0"/>
                        <xs:element name="ESTIMATED_TIME" type="xs:string" 
minOccurs="0"/>
                        <xs:element name="JEP_CODE" type="xs:string" minOccurs="0"/>
                        <xs:element name="COMPTD_DATE_TIME" type="xs:dateTime" 
minOccurs="0"/>
<xs:element name="CUST_NAME" type="xs:string" minOccurs="0"/> <xs:element
name="REPORT1" type="xs:string" minOccurs="0" maxOccurs="1"/>
                        <xs:element name="REPORT2" type="xs:string" minOccurs="0"

maxOccurs="1"/>


                        <xs:element name="STATUS_DATE_TIME" type="xs:dateTime" 
minOccurs="0"/>
                        <xs:element name="RPTCAT" type="xs:string" minOccurs="0"/>
                        <xs:element name="TROUBLE_FOUND1" type="xs:string" 
minOccurs="0"
maxOccurs="1"/>
                        <xs:element name="TROUBLE_FOUND2" type="xs:string" 
minOccurs="0"
maxOccurs="1"/>
                        <xs:element name="TROUBLE_FOUND3" type="xs:string" 
minOccurs="0"
maxOccurs="1"/>
                        <xs:element name="NDPN" type="xs:string" minOccurs="0"/>
                        <xs:element name="CTL_RMKS" type="xs:string" minOccurs="0"/>
                        <xs:element name="CTL_RMKS1" type="xs:string" minOccurs="0"/>
                        <xs:element name="CTL_RMKS2" type="xs:string" minOccurs="0"/>
                        <xs:element name="CTL_RMKS3" type="xs:string" minOccurs="0"/>
                        <xs:element name="CTL_RMKS4" type="xs:string" minOccurs="0"/>
                        <xs:element name="COMMENTS" type="xs:string" minOccurs="0"/>
                        <xs:element name="CIRCUIT_ACTION" type="xs:string" 
minOccurs="0"/>
                        <xs:element name="NUM_CHILD" type="xs:string" minOccurs="0"/>
                        <xs:element name="MANUAL_TRICK" type="xs:string" 
minOccurs="0"/>
                        <xs:element name="PW_JOBID" type="xs:string" minOccurs="0"/>
                        <xs:element name="FMTID" type="xs:string" minOccurs="0"/>
                        <xs:element name="CRITICAL_DATE_ID" type="xs:string" 
minOccurs="0"/>
                        <xs:element name="CRITICAL_DATE_VALUE" type="xs:string" 
minOccurs="0"/>
                </xs:all>
        </xs:complexType>
</xs:schema>

thanks and regards,

Naveen Srinivasa Murthy



-------------------------------------------------------
This SF.Net email is sponsored by: INetU
Attention Web Developers & Consultants: Become An INetU Hosting Partner.
Refer Dedicated Servers. We Manage Them. You Get 10% Monthly Commission!
INetU Dedicated Managed Hosting http://www.inetu.net/partner/index.php
_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user




--


+------------------------------------------------------------+
| David Lucas                        mailto:[EMAIL PROTECTED]  |
| Lucas Software Engineering, Inc.   (740) 964-6248 Voice    |
| Unix,Java,C++,CORBA,XML,EJB        (614) 668-4020 Mobile   |
| Middleware,Frameworks              (888) 866-4728 Fax/Msg  |
+------------------------------------------------------------+
| GPS Location:  40.0150 deg Lat,  -82.6378 deg Long         |
| IMHC: "Jesus Christ is the way, the truth, and the life."  |
| IMHC: "I know where I am; I know where I'm going."    <><  |
+------------------------------------------------------------+

Notes: PGP Key Block=http://www.lse.com/~ddlucas/pgpblock.txt
IMHO="in my humble opinion" IMHC="in my humble conviction"
All trademarks above are those of their respective owners.




------------------------------------------------------- This SF.Net email is sponsored by: INetU Attention Web Developers & Consultants: Become An INetU Hosting Partner. Refer Dedicated Servers. We Manage Them. You Get 10% Monthly Commission! INetU Dedicated Managed Hosting http://www.inetu.net/partner/index.php _______________________________________________ dom4j-user mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/dom4j-user



-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100006ave/direct;at.asp_061203_01/01
_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user



--

+------------------------------------------------------------+
| David Lucas                        mailto:[EMAIL PROTECTED]  |
| Lucas Software Engineering, Inc.   (740) 964-6248 Voice    |
| Unix,Java,C++,CORBA,XML,EJB        (614) 668-4020 Mobile   |
| Middleware,Frameworks              (888) 866-4728 Fax/Msg  |
+------------------------------------------------------------+
| GPS Location:  40.0150 deg Lat,  -82.6378 deg Long         |
| IMHC: "Jesus Christ is the way, the truth, and the life."  |
| IMHC: "I know where I am; I know where I'm going."    <><  |
+------------------------------------------------------------+

Notes: PGP Key Block=http://www.lse.com/~ddlucas/pgpblock.txt
IMHO="in my humble opinion" IMHC="in my humble conviction"
All trademarks above are those of their respective owners.




------------------------------------------------------- This SF.Net email sponsored by: Free pre-built ASP.NET sites including Data Reports, E-commerce, Portals, and Forums are available now. Download today and enter to win an XBOX or Visual Studio .NET. http://aspnet.click-url.com/go/psa00100006ave/direct;at.asp_061203_01/01 _______________________________________________ dom4j-user mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to