using presto APIs with Java and xpath

User offline. Last seen 39 weeks 2 days ago. Offline
Joined: 12/08/2008
Points: 10
Groups: None

i am new to presto.  i created a simple mashup called DoSomething with one operation getData which returns the NNMoneyHeadlines.

I used the display tag to filter the results which works; here is the emml:

<mashup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.jackbe.com/2008-03-01/EMMLSchema ../src/schemas/EMMLSpec.xsd"
    xmlns="http://www.jackbe.com/2008-03-01/EMMLSchema" xmlns:macro="http://www.jackbe.com/2008-03-01/EMMLMacro"
    name="DoSomething">

    <operation name="getData">
        <output name="resultFinal" type="document" />

        <invoke service="CNNMoneyHeadlines" operation="getFeed" outputvariable="resultTemp" />

      <foreach items="$resultTemp/*:rss/channel/item" variable="item">
         <assign fromexpr="$item/*:title/string()" outputvariable="title" />
         <assign fromexpr="$item/*:link/string()" outputvariable="link" />
            <appendresult outputvariable="resultFinal">
              <item> 
                    <title>{ $title }</title>
                    <link> { $link } </link>
              </item>
            </appendresult>
      </foreach>
     
    </operation>

</mashup>

 

when using the presto java APIs i get the following output:

#####
 nodes.length = 20
#####
0: currentNode = [item: null]
1: currentNode = [item: null]
2: currentNode = [item: null]
3: currentNode = [item: null]
4: currentNode = [item: null]
5: currentNode = [item: null]
6: currentNode = [item: null]
7: currentNode = [item: null]
8: currentNode = [item: null]
9: currentNode = [item: null]
10: currentNode = [item: null]
11: currentNode = [item: null]
12: currentNode = [item: null]
13: currentNode = [item: null]
14: currentNode = [item: null]
15: currentNode = [item: null]
16: currentNode = [item: null]
17: currentNode = [item: null]
18: currentNode = [item: null]
19: currentNode = [item: null]

 

the java code is here:

package mashuptest.simplemashup;

import java.io.StringReader;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.xml.sax.InputSource;
import org.w3c.dom.Node;

import com.sun.org.apache.xml.internal.dtm.ref.DTMNodeList;
import com.sun.org.apache.xml.internal.dtm.DTMIterator;

import com.jackbe.presto.connect.Connection;
import com.jackbe.presto.connect.ConnectionFactory;
import com.jackbe.presto.connect.JUMPRequest;
import com.jackbe.presto.connect.JUMPResponse;
import com.jackbe.presto.connect.common.PrestoConstants;

/**
 * @author mpurdy
 *
 */
public class PrestoTest
{

   public void doMashupTest()
   {
      JUMPRequest jumpRequest = new JUMPRequest();
      String prestoUrl = "http://localhost:8080/presto/edge/api";
      //create remote connection
      Connection connection = ConnectionFactory.create(prestoUrl);

        
      String authToken = connection.login("admin", "adminadmin");
     
      jumpRequest.setVersion(PrestoConstants.JUMP_PROTOCOL_1_1);
      jumpRequest.setSid("DoSomething");
      jumpRequest.setOid("getData");
      jumpRequest.setSvcVersion("0.1");
        
      jumpRequest.setHeaderParameter(PrestoConstants.JUMP_RESPONSE_AUTH_TOKEN, authToken);
        
      //optional header
      jumpRequest.setHeaderParameter(PrestoConstants.JUMP_REQUEST_HEADER_RESULT_FORMAT, PrestoConstants.JUMP_RESULT_FORMAT_XML);
     
     
      JUMPResponse jumpResponse = connection.invoke(jumpRequest);
     
      XPath xpath = XPathFactory.newInstance().newXPath();
      String expression = "/rss/channel/item";
      InputSource inputSource = new InputSource(new StringReader((String)jumpResponse.getResponse()));
      DTMNodeList nodes = null;
      try
      {
         nodes = (DTMNodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
      }
      catch(XPathExpressionException e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }

      if(nodes != null)
      {
System.out.println("#####\n nodes.length = " + nodes.getLength() + "\n#####");
         Node node = null;
         for(int i = 0; i < nodes.getLength(); i++)
         {
            node = nodes.item(i);
            System.out.println("" + i + ": currentNode = " + node);
           
         }//end for all nodes      
        
      }//end if there are nodes
  
   }//end method doMashupTest
  
}//end class PrestoTest
 

 

what am i doing wrong in the java to get null nodes?

 

attached is a zip file containing the two files.

 

thanx,

matt

 

 

 

 

 

0
Your rating: None

I haven't tested this, but I think it is your XPath expression. And there is perhaps one problem in the EMML for the mashup itself.

Have you tried debugging the EMML script in Mashup Studio? This would allow you to set the output from the mashup -- which I am quite certain no longer contains /rss/channel/item.

If you're working from the command line, you can add <display> statements to see the value of your output variable. Then test the mashup from the command line to see the actual output.

The output from your mashup should actually be something like this: 

<xml>
<item><title>something</title><link>some url</link</item>
<item>...</item>
...
</xml>

<appendresult> in the EMML actually builds the structure of the result that the mashup service creates.

So this line in your Java:

  String expression = "/rss/channel/item";

is the problem there, as /rss/channel/item doesn't exist in the results. You need to modify this to match the actual structure of the mashup output.
 

Sara, technical writer/jackbe

 

raj
raj's picture
User offline. Last seen 6 days 6 hours ago. Offline
Joined: 09/22/2008
Points: 2
Groups: None

Hi Matt,

You seem to be getting the results, the 'null' is because Item does not have any direct text content (i.e. it has only child elements). So, use DOM API to traverse Item nodes child elements, you should see the results in those child nodes.

raj.  chief masher @ jackbe

User offline. Last seen 39 weeks 2 days ago. Offline
Joined: 12/08/2008
Points: 10
Groups: None

thanx for the input.

 

you are correct the java code i posted would not work with /rss/channel/item with the posted wmml.  i change the wmml and run the script without publishing it to the server.  initially the wmml only returned the rss feed.

 

i am new to using xpath in java.  i learned that you need to use node.getTextContent() to get the text from /xml/item

User offline. Last seen 39 weeks 2 days ago. Offline
Joined: 12/08/2008
Points: 10
Groups: None

yes, you are right; i changed my code and now i can get the text.

 

**
 *
 */
package mashuptest.simplemashup;

import java.io.StringReader;
import java.util.StringTokenizer;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.xml.sax.InputSource;
import org.w3c.dom.Node;

import com.sun.org.apache.xml.internal.dtm.ref.DTMNodeList;
import com.sun.org.apache.xml.internal.dtm.DTMIterator;

import com.jackbe.presto.connect.Connection;
import com.jackbe.presto.connect.ConnectionFactory;
import com.jackbe.presto.connect.JUMPRequest;
import com.jackbe.presto.connect.JUMPResponse;
import com.jackbe.presto.connect.common.PrestoConstants;

/**
 * @author mpurdy
 *
 */
public class PrestoTest
{

   public void doMashupTest()
   {
      JUMPRequest jumpRequest = new JUMPRequest();
      String prestoUrl = "http://localhost:8080/presto/edge/api";
      //create remote connection
      Connection connection = ConnectionFactory.create(prestoUrl);

        
      String authToken = connection.login("admin", "adminadmin");
     
      jumpRequest.setVersion(PrestoConstants.JUMP_PROTOCOL_1_1);
      jumpRequest.setSid("DoSomething");
      jumpRequest.setOid("getData");
      jumpRequest.setSvcVersion("0.1");
        
      jumpRequest.setHeaderParameter(PrestoConstants.JUMP_RESPONSE_AUTH_TOKEN, authToken);
        
      //optional header
      jumpRequest.setHeaderParameter(PrestoConstants.JUMP_REQUEST_HEADER_RESULT_FORMAT, PrestoConstants.JUMP_RESULT_FORMAT_XML);
     
     
      JUMPResponse jumpResponse = connection.invoke(jumpRequest);
     
      XPath xpath = XPathFactory.newInstance().newXPath();
      String expression = "/xml/item";
      InputSource inputSource = new InputSource(new StringReader((String)jumpResponse.getResponse()));
      DTMNodeList nodes = null;
      try
      {
         nodes = (DTMNodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
      }
      catch(XPathExpressionException e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }

      if(nodes != null)
      {
System.out.println("#####\n nodes.length = " + nodes.getLength() + "\n#####");
         Node node = null;
         for(int i = 0; i < nodes.getLength(); i++)
         {
            node = nodes.item(i);
            StringTokenizer st = new StringTokenizer(node.getTextContent(), "\n");
           
            while(st.hasMoreTokens())
               System.out.println(st.nextToken());
           
            System.out.println("");
         }//end for all nodes      
        
      }//end if there are nodes
  
   }//end method doMashupTest
  
}//end class PrestoTest