[jdom-interest] Validation strategy

Thomas Scheffler thomas.scheffler at uni-jena.de
Wed May 10 04:42:35 PDT 2006


Am Mittwoch, 10. Mai 2006 12:29 schrieb Søren Faltz:
> I am the client, i receive messages from the server.
>
> this is a sample of the message I would receive
>
>
>   <?xml version="1.0" encoding="UTF-8" ?>
>   - <#> <Envelope
> xmlns:xsi="*http://www.w3.org/2001/XMLSchema-instance*"xsi:noNamespaceSchem
>aLocation ="*schema1.xsd*">
>  - <#> <Header>
>    <tDateTime>2006-01-17T08:42:17+08:00</DateTime>
> <data>hello world</data>
>
> </Header>- <#>
> </Envelope>
>
>
> So what is this entityresolver that you are talking about?? Could I use
> that to determine the correct schema???

http://java.sun.com/j2se/1.4.2/docs/api/org/xml/sax/EntityResolver.html

Set the Entity resolver used by the SAXBuilder:

builder.setEntityResolver( new MyEntityResolver() );

Yes, implement the method resolveEntity like this:

public InputSource resolveEntity(String publicId, String systemId) throws org.xml.sax.SAXException, java.io.IOException {
        if (systemId == null) {
            return null; // Use default resolver
        }
        //try to read schema file from classpath
        InputStream is = this.getClass().getResourceAsStream("/" + systemId);
        if (is == null) {
            return null; // Use default resolver
        }
        return new InputSource(is);
}

If this solves the problem you some kind of cache for the getResourceAsStream call and make the EntityResolver a stateless Singleton to enhance perfomance.

regards

Thomas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.jdom.org/pipermail/jdom-interest/attachments/20060510/450ff353/attachment.htm


More information about the jdom-interest mailing list