Reading XML results from another server within a JSP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Angus McPresley

    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 programmaticall y.

    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 EmptyStackExcep tion'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

  • Joe Kesselman

    #2
    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

    Comment

    • Angus McPresley

      #3
      Re: Reading XML results from another server within a JSP

      Joe Kesselman wrote:
      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
      EmptyStackExcep tion. 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(ur l.openStream()) ;
      StreamSource xsl = new StreamSource(st rXSL);

      //StreamResult result = new StreamResult(ou t);
      DOMResult result = new DOMResult();

      TransformerFact ory tFactory =
      TransformerFact ory.newInstance ();
      Transformer transformer = tFactory.newTra nsformer(xsl);
      transformer.tra nsform(xml, result);
      if ( xml != null )
      {
      Reader xmlReader = xml.getReader() ;
      if ( xmlReader != null )
      {
      out.print(xmlRe ader.toString() );
      }
      }
      }
      catch ( Exception e )
      {
      out.print( "<br/><b>Forms and publications search is" +
      " currently unavailable. Please contact the" +
      " administrator if problem persists.</b>" );
      //e.printStackTra ce(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>

      Comment

      • Angus McPresley

        #4
        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
        EmptyStackExcep tion. Pretty stoopid of me.

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


        Angus McPresley wrote:
        Joe Kesselman wrote:
        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
        EmptyStackExcep tion. 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(ur l.openStream()) ;
        StreamSource xsl = new StreamSource(st rXSL);
        >
        //StreamResult result = new StreamResult(ou t);
        DOMResult result = new DOMResult();
        >
        TransformerFact ory tFactory =
        TransformerFact ory.newInstance ();
        Transformer transformer = tFactory.newTra nsformer(xsl);
        transformer.tra nsform(xml, result);
        if ( xml != null )
        {
        Reader xmlReader = xml.getReader() ;
        if ( xmlReader != null )
        {
        out.print(xmlRe ader.toString() );
        }
        }
        }
        catch ( Exception e )
        {
        out.print( "<br/><b>Forms and publications search is" +
        " currently unavailable. Please contact the" +
        " administrator if problem persists.</b>" );
        //e.printStackTra ce(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>

        Comment

        • Joe Kesselman

          #5
          Re: Reading XML results from another server within a JSP

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

          Comment

          Working...