Reading XML results from another server within a JSP

A

Angus McPresley

From within a JSP, I need to hit another server to retrieve some XML
results, then parse them programmatically.

If I use a StreamResult object, I can transform the results I get back
using XSL just fine. But, to get at the individual nodes of the XML
tree, if I switch to a DOMResult object, I get EmptyStackException's.
From looking around, I surmised this is because DOMResult objects only
work on files, not on text (which I retrieve from the external URL).

So, how would I go about parsing the individual nodes in the XML
retrieved from an external server, from within a JSP? StreamSource
won't let me at the nodes themselves.

Hope this makes sense. Any help in this matter would be MUCH
appreciated...


--Mark
 
J

Joe Kesselman

I don't know about JSPs, but the JAXP/TrAX APIs *should* transform from
any of the Source variants to any of the Result classes. This sounds
like a bug -- either in how you're configuring these processing stages,
or perhaps you're using an out-of-date version of the code.

Reminder: If you want to put a DOM, or a pre-parsed SAX event stream,
through XSLT you need to be *very* sure you've turned on all the
relevant namespace features of the parser.

To say more than that I'd have to see the code. I don't do JSPs, but
presumably the stuff that's failing can also be demonstrated in a
one-page stand-alone example...?
 
A

Angus McPresley

Joe said:
I don't know about JSPs, but the JAXP/TrAX APIs *should* transform from
any of the Source variants to any of the Result classes. This sounds
like a bug -- either in how you're configuring these processing stages,
or perhaps you're using an out-of-date version of the code.

Reminder: If you want to put a DOM, or a pre-parsed SAX event stream,
through XSLT you need to be *very* sure you've turned on all the
relevant namespace features of the parser.

To say more than that I'd have to see the code. I don't do JSPs, but
presumably the stuff that's failing can also be demonstrated in a
one-page stand-alone example...?

Below is a snippet of the relevant code. If I comment out the
DOMResult line and
uncomment the line below it it works fine. With the DOMResult line I
get the
EmptyStackException. The XML that it's trying to process as a result
of the call
to the other server is tacked on the end.

String strXML = "http://myserver.com/whatever";
String strXSL = "path/to/my.xsl";
URL url = new URL(strXML);
StreamSource xml=null;
try
{
xml = new StreamSource(url.openStream());
StreamSource xsl = new StreamSource(strXSL);

//StreamResult result = new StreamResult(out);
DOMResult result = new DOMResult();

TransformerFactory tFactory =
TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(xsl);
transformer.transform(xml, result);
if ( xml != null )
{
Reader xmlReader = xml.getReader();
if ( xmlReader != null )
{
out.print(xmlReader.toString());
}
}
}
catch ( Exception e )
{
out.print( "<br/><b>Forms and publications search is" +
" currently unavailable. Please contact the" +
" administrator if problem persists.</b>" );
//e.printStackTrace(new PrintWriter(out));
}



XML it's trying to process looks like this. This is not something I
can modify, btw,
as it's from an off-the-shelf search aggregator.

<?xml version="1.0" encoding="UTF-8"?>
<searchdoc>
<results hits="31" time="0.01" query="keywords:vwadocument AND work"
suggest="" filter="" sort="relevance" start="1" end="10"
currentpage="1" lastpage="4" startdate="0" xsl="xml">
<result no="1">[...]</result>
[...etc...]
</results>
</searchdoc>
 
A

Angus McPresley

Solved this, btw... When I created the Transformer, I shouldn't have
been passing
in the XSL parameter -- all I needed was the no-argument constructor.
It was trying
to parse the XSL-transformed XML, I think, which was why it was getting
the
EmptyStackException. Pretty stoopid of me.

Thanks, Joe, for at least keeping from keeping on the right track...


Angus said:
Joe said:
I don't know about JSPs, but the JAXP/TrAX APIs *should* transform from
any of the Source variants to any of the Result classes. This sounds
like a bug -- either in how you're configuring these processing stages,
or perhaps you're using an out-of-date version of the code.

Reminder: If you want to put a DOM, or a pre-parsed SAX event stream,
through XSLT you need to be *very* sure you've turned on all the
relevant namespace features of the parser.

To say more than that I'd have to see the code. I don't do JSPs, but
presumably the stuff that's failing can also be demonstrated in a
one-page stand-alone example...?

Below is a snippet of the relevant code. If I comment out the
DOMResult line and
uncomment the line below it it works fine. With the DOMResult line I
get the
EmptyStackException. The XML that it's trying to process as a result
of the call
to the other server is tacked on the end.

String strXML = "http://myserver.com/whatever";
String strXSL = "path/to/my.xsl";
URL url = new URL(strXML);
StreamSource xml=null;
try
{
xml = new StreamSource(url.openStream());
StreamSource xsl = new StreamSource(strXSL);

//StreamResult result = new StreamResult(out);
DOMResult result = new DOMResult();

TransformerFactory tFactory =
TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(xsl);
transformer.transform(xml, result);
if ( xml != null )
{
Reader xmlReader = xml.getReader();
if ( xmlReader != null )
{
out.print(xmlReader.toString());
}
}
}
catch ( Exception e )
{
out.print( "<br/><b>Forms and publications search is" +
" currently unavailable. Please contact the" +
" administrator if problem persists.</b>" );
//e.printStackTrace(new PrintWriter(out));
}



XML it's trying to process looks like this. This is not something I
can modify, btw,
as it's from an off-the-shelf search aggregator.

<?xml version="1.0" encoding="UTF-8"?>
<searchdoc>
<results hits="31" time="0.01" query="keywords:vwadocument AND work"
suggest="" filter="" sort="relevance" start="1" end="10"
currentpage="1" lastpage="4" startdate="0" xsl="xml">
<result no="1">[...]</result>
[...etc...]
</results>
</searchdoc>
 
J

Joe Kesselman

Angus said:
Thanks, Joe, for at least keeping from keeping on the right track...

Sorry I didn't have time to follow up on this; glad you've got it under
control.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top