You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@ws.apache.org by Paul <pa...@gmail.com> on 2011/01/11 15:05:45 UTC

Telling AXIOM where to find a DTD

Hi:

I'm hoping someone can point me in the right direction. I'm using AXIOM to
parse an XML payload returned to me by a server (over HTTP). The
payload looks something like this:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE example SYSTEM "example.dtd">
   <example version="1.0">
      <response>
         <code>001</code>
         <message>This is some helpful message</message>
      </response>
   </example>

The received payload is stored to a string and I am trying to use *AXIOMUtil
*.*stringToOM* to build up my OMElement. The problem I am observing is that
AXIOM is trying to locate the "example.dtd" file presumably to validate the
contents. However, it seems to look no further than the folder containing
the JVM and so throws an exception. I have the DTD file, but I need a way to
direct AXIOM to the correct location. Oh, and when I manually copy the DTD
to the JVM's folder AXIOM is fine. However, this is not a practical solution
for production.

I've been looking through the javadocs for an alternate approach, but can't
seem to find anything that provides what I'm looking for. A nudge in the
right direction would be greatly appreciated.

Cheers,
Paul

Re: Telling AXIOM where to find a DTD

Posted by Paul <pa...@gmail.com>.
Thanks Andreas, that was exactly what I needed! I knew it had to something
along those lines, but just couldn't seem to find the right path through the
API. I've ended up implementing a custom XMLResolver and pull the DTD out of
a jar on the fly.

Once again, thanks!
Paul

Re: Telling AXIOM where to find a DTD

Posted by Andreas Veithen <an...@gmail.com>.
Paul,

AXIOMUtil.stringToOM was designed as a utility method for very simple
cases. It won't allow you to handle DTDs correctly. Instead you should
use the StAXOMBuilder API as described in [1]. However, you need to
make sure that the XMLStreamReader you pass to StAXOMBuilder is
correctly set up. You basically have two options:

1. You don't care about the DTD (The DTD may be used for validation,
and is always used to augment the XML infoset with things such as
default attribute values). If you don't need this, you can create the
XMLStreamReader using one of the methods in
org.apache.axiom.om.util.StAXUtils together with
StAXParserConfiguration.STANDALONE. This makes sure that the parser
doesn't read the DTD.

2. You want the parser to read and process the DTD. In that case, you
should create the XMLStreamReader directly using the StAX API, i.e.
with the help of XMLInputFactory. To allow the parser to find the DTD,
you need to pass a systemId (so that the parser can load the DTD
relative to that location) or set up an XMLResolver. If you neither
provide a systemId nor an XMLResolver, then the parser will attempt to
locate the DTD relative to the current working directory. This is the
behavior that you are seeing.

Andreas

[1] http://ws.apache.org/axiom/userguide/ch02.html#creation

On Tue, Jan 11, 2011 at 15:05, Paul <pa...@gmail.com> wrote:
> Hi:
>
> I'm hoping someone can point me in the right direction. I'm using AXIOM to
> parse an XML payload returned to me by a server (over HTTP). The
> payload looks something like this:
>
> <?xml version="1.0" encoding="UTF-8" ?>
> <!DOCTYPE example SYSTEM "example.dtd">
>    <example version="1.0">
>       <response>
>          <code>001</code>
>          <message>This is some helpful message</message>
>       </response>
>    </example>
>
> The received payload is stored to a string and I am trying to use
> AXIOMUtil.stringToOM to build up my OMElement. The problem I am observing is
> that AXIOM is trying to locate the "example.dtd" file presumably to validate
> the contents. However, it seems to look no further than the folder
> containing the JVM and so throws an exception. I have the DTD file, but I
> need a way to direct AXIOM to the correct location. Oh, and when I manually
> copy the DTD to the JVM's folder AXIOM is fine. However, this is not a
> practical solution for production.
>
> I've been looking through the javadocs for an alternate approach, but can't
> seem to find anything that provides what I'm looking for. A nudge in the
> right direction would be greatly appreciated.
>
> Cheers,
> Paul