tags:

views:

345

answers:

2

I'm searching a way to populate a ListGrid with an XML response from a server.

This is an example of my server response:

<?xml version="1.0" encoding="UTF-8"?>
  <tbody id="tbody">
    <tr>
      <word>The</word>
      <frequence>12</frequence>
    </tr>
    <tr>
       <word>best</word>
       <frequence>3</frequence>
    </tr>
    ...

And this is how I can populate the ListGrid using a file (item.xml) where I have saved the xml result:

public class Frequenze extends DataSource {
    private static Frequenze instance = null;

      public static Frequenze getInstance() 
      {
        if (instance == null) {
            instance = new Frequenze("frequence");
        }
        return instance;
      }

      public Frequenze(String id) 
      {
            setID(id);
            setRecordXPath("//tr");
            DataSourceTextField wordField = new DataSourceTextField("word", "Word");
            wordField.setRequired(true);

            DataSourceIntegerField frequenceField = new DataSourceIntegerField("frequence", "Frequence");
            frequenceField.setRequired(true);

            setFields(wordField, frequenceField);

            setDataURL("ds/item.xml");
            setClientOnly(true);
      }
}

Now I want not to use the file, but I'm searching a way to retrieve the data directly from the server. Anyone know how I get this?

+1  A: 

You could consider creating a Servlet on your server that responds with the XML based on query parameters in the request.

Everett Toews
A: 

I second Everett Toews, you can set the data-url to

"http://yourServer:8080/webappName/servletName?query=yourQuery"

All you have to do now is getting the param you passed to the servlet (in this case "query") and read its value. Depending on its value you can modify the response like:

PrintWriter out = resp.getWriter();
out.print(xml);
out.flush();

Writing such a servlet is fairly easy - just extend the javax.servlet.http.HttpServlet class and implement the javax.servlet.Servlet class.

By overriding the method doGet() you can do anything you may want to do.

The last thing you may want to do is create a servlet-mapping in the web.xml like

<servlet>
    <servlet-name>myXmlServlet</servlet-name>
    <servlet-class>your.package.servlets.MyXMLServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>myXmlServlet</servlet-name>
    <url-pattern>/query2xml</url-pattern>
</servlet-mapping>
Moritz