"Peter A. Kirk" <p...@alpha-solutions.dk> wrote on 11/07/2010 10:09:14 PM:

> OK, thanks. I've been trying to read the w3.org descriptions of
> include and fallback, and I was coming to the conclusion that it wasby
design.
>
> Unfortunate for me though, because I'd like "all error fallback".
>
> I have for example some http requests for included xml, which the
> server cannot process sometimes (maybe the supplied url-parameters
> are incorrect, or a database error occurs at the server end) - and
> the server can have no way to know what "valid" xml to return. So I
> just tried to get it to return "empty" xml - but found this could
> not be parsed, and had hoped that the "fallback" would then handle it.

To get the fallback what you really need here is an IOException, not an
empty file but some kind of failure to read from the URL. You could even
register an EntityResolver and force an IOException to be thrown from it
based on something you read from the HTTP response. This IOException will
be caught by the XInclude processor, reported as a resource error and then
the fallback you specified will be used.

> For example, my "GeneralConfig" web-app could be called like this:
>
> <xi:include href="http://localhost/GeneralConfig?cfg=abc_config"; >
> </xi:include>
>
> And it should read the config from a database which matches the
> "cfg=abc_config" parameter, and return it to the caller.
>
> But, if there is no config matching this name in the database, or if
> the database is down for example, then I just returned nothing. But
> this causes the whole xml parsing to fail.
>
> /Peter
>
> From: Michael Glavassevich [mrgla...@ca.ibm.com]
> Sent: Monday, 8 November 2010 15:53
> To: j-users@xerces.apache.org
> Subject: Re: How to use xi include and fallback
>
> Hi Peter,
>
> The fallback is only used in the event of a resource error [1]. For
> example, the file doesn't exist or possibly there's some kind of
> connection problem which prevents the parser from accessing the URI
> (specified as the value of href). Well formedness errors within the
> included XML document are always fatal.
>
> Thanks.
>
> [1] http://www.w3.org/TR/xinclude/#dt-resource-error
>
> Michael Glavassevich
> XML Parser Development
> IBM Toronto Lab
> E-mail: mrgla...@ca.ibm.com
> E-mail: mrgla...@apache.org
>
> "Peter A. Kirk" <p...@alpha-solutions.dk> wrote on 11/07/2010 07:50:09 PM:
>
> > Hi
> >
> > I'm new to the Xerces xml parser, and would like some help loading/
> > parsing xml documents using Xi Include.
> >
> > I have two xml files, "mainxml.xml" and "includexml.xml".
> > MainXml looks like this:
> > <?xml version="1.0" encoding="utf-8" ?>
> > <config xmlns:xi="http://www.w3.org/2001/XInclude";>
> >  <abc>
> >   <xi:include href="includexml.xml">
> >    <xi:fallback>
> >     <str name="fallback include">
> >       failure include
> >     </str>
> >    </xi:fallback>
> >   </xi:include>
> >  </abc>
> > </config>
> >
> > and IncludeXml looks like:
> > <str name="include">asd</str>
> >
> >
> > I'm trying to load the MainXml file like this:
> >
> > DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
> > dbf.setNamespaceAware(true);
> > dbf.setXIncludeAware(true);
> >
> > DocumentBuilder db = dbf.newDocumentBuilder();
> >
> > Document doc = db.parse("mainxml.xml");
> >
> >
> > Which works ok, unless there is an error in "includexml.xml". If
> > there is an error, then the fallback is not used - why not? An error
> > could be en empty "includexml.xml" file, or if the xml was invalid.
> >
> > How do I get the fallback to be used?
> >
> >
> >
> > Thanks,
> > Peter

Thanks.

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrgla...@ca.ibm.com
E-mail: mrgla...@apache.org

Reply via email to