Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 28th, 2006, 03:05 AM
Angus McPresley
Guest
 
Posts: n/a
Default Reading XML results from another server within a JSP

>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.
Quote:
>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

  #2  
Old July 28th, 2006, 04:05 AM
Joe Kesselman
Guest
 
Posts: n/a
Default Re: Reading XML results from another server within a JSP

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...?

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
  #3  
Old July 28th, 2006, 05:35 AM
Angus McPresley
Guest
 
Posts: n/a
Default Re: Reading XML results from another server within a JSP

Joe Kesselman wrote:
Quote:
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>

  #4  
Old July 31st, 2006, 05:35 AM
Angus McPresley
Guest
 
Posts: n/a
Default Re: Reading XML results from another server within a JSP

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 McPresley wrote:
Quote:
Joe Kesselman wrote:
Quote:
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>
  #5  
Old July 31st, 2006, 01:15 PM
Joe Kesselman
Guest
 
Posts: n/a
Default Re: Reading XML results from another server within a JSP

Angus McPresley wrote:
Quote:
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.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles