Link to home
Start Free TrialLog in
Avatar of fortneci
fortneci

asked on

YUI DataTable with Data Source as a Java Servlet generating XML

I am working to implement the YUI DataTable (http://developer.yahoo.com/yui/datatable/) with my data source as a Java servlet that creates XML output.

What I know:
outputter.outputString(jDOMDoc); will output the XML structured as:
<ResultSet><Result><ROUNDS>3</ROUNDS> .....  </Result></ResultSet>
I get the Javascript error 'no element found' http://url/LeagueStandings?get=all&ouput=xml
The table appears with the column headers, but the data does not appear.
No Exceptions are being thrown.

The Servlet Code:
           RStoXML rsxml = new RStoXML(results, "ResultSet", "Result");
           //create the XML from recordset
            Document jDOMDoc = rsxml.build();            
            results.close();
            XMLOutputter outputter = new XMLOutputter() ;
            StringWriter writer = new StringWriter() ;
            outputter.output( jDOMDoc, writer );
            writer.close() ;
            //respond with output stream
            rep.setContentType("text/xml");
            rep.getOutputStream();

Javscript:

var myColumnHeaders = [
    {key:"ROUNDS",text:"ROUNDS"},
    {key:"USERNAME",text:"USER"},
    {key:"BIRDIES",text:"BIRDIES"},
    {key:"PARS",text:"PARS"},
    {key:"BOGIES",text:"BOGIES"},
    {key:"DOUBLES",text:"DOUBLES"},
    {key:"GH",text:"GH"},
    {key:"FH",text:"FH"},
    {key:"PUTTS",text:"PUTTS"},
    {key:"SCORES",text:"SCORES"},
];
var myColumnSet = new YAHOO.widget.ColumnSet(myColumnHeaders);

// Show over 1000 records
var myDataSource = new YAHOO.util.DataSource("http://url/LeagueStandings");

// Set the responseType as XML
myDataSource.responseType = YAHOO.util.DataSource.TYPE_XML;

// Define the data schema
myDataSource.responseSchema = {
    resultNode: "ResultSet.Result", // Node name of each result item
    fields: ["ROUNDS","USER","BIRDIES","PARS","BOGIES","DOUBLES","GH","FH","PUTTS","SCORES"] // Field names
};
//Pagination
var initialRequest = "get=all&output=xml";
var oConfigs = {
        caption:"Example: Paginated Over XHR",
        initialRequest:initialRequest,

        pageCurrent: 1,
        rowsPerPage: 100,
        startRecordIndex: 1,
        pageLinksLength: 10,
        rowsPerPageDropdown: [25,50,100,500]
};
var myDataTable = new YAHOO.widget.DataTable("paginated", myColumnSet, myDataSource, oConfigs);

Avatar of stanscott2
stanscott2

I'm not sure this goes the whole nine yards, but it appears that you have an error in the fields array.  You're showing "USER", when the name of the key is "USERNAME"
Avatar of fortneci

ASKER

I changed it all to USERNAME to clear up any discrepancy, but the result is the same. I still get the 'no element found' http://url/LeagueStandings?get=alll&output=xml JAVASCRIPT ERROR. My thought is that I am getting the error because my XML data source  isn't being read. I validated in the Servlet that the jDOMDoc is creating XML, but I haven't been able to validate that it is being returned in the response of the servlet, any suggestions on how to do that?
Avatar of Mayank S
Can you post the XML?
<?xml version="1.0" encoding="UTF-8"?>
<ResultSet>
    <Result>
         <ROUNDS>3</ROUNDS>
         <USERNAME>testuser</USERNAME>
         <BIRDIES>2</BIRDIES>
         <PARS>13</PARS>
         <BOGIES>28</BOGIES>
         <DOUBLES>11</DOUBLES>
         <GH>10</GH>
        <FH>10</FH>
        <PUTTS>108</PUTTS>
        <SCORES>264</SCORES>
    </Result>
</ResultSet>
If i just try to hit the servlet, I am getting the following error.

XML Parsing Error: no element found
Location: http://url/LeagueStandings
Line Number 1, Column 1:
Found the problem and got this resolved. I am posting the updated code for the knowledge base.

XML:
<?xml version="1.0" encoding="UTF-8"?>
<ResultSet>
    <Result>
         <ROUNDS>3</ROUNDS>
         <USERNAME>testuser</USERNAME>
         <BIRDIES>2</BIRDIES>
         <PARS>13</PARS>
         <BOGIES>28</BOGIES>
         <DOUBLES>11</DOUBLES>
         <GH>10</GH>
        <FH>10</FH>
        <PUTTS>108</PUTTS>
        <SCORES>264</SCORES>
    </Result>
</ResultSet>

Servlet Code:
Error was due to not writing it to the response.
            RStoXML rsxml = new RStoXML(results, "ResultSet", "Result");
            //create the XML from recordset
            Document jDOMDoc = rsxml.build();            
            results.close();
            XMLOutputter outputter = new XMLOutputter() ;
            StringWriter writer = new StringWriter() ;
            outputter.output( jDOMDoc, writer );
            writer.close() ;
            //respond with output stream
            rep.setContentType("text/xml");
            rep.getWriter().println(writer.toString());


YUI Javascript:
The error was with resultNode

var myColumnHeaders = [
    {key:"ROUNDS",text:"ROUNDS"},
    {key:"USERNAME",text:"USERNAME"},
    {key:"BIRDIES",text:"BIRDIES"},
    {key:"PARS",text:"PARS"},
    {key:"BOGIES",text:"BOGIES"},
    {key:"DOUBLES",text:"DOUBLES"},
    {key:"GH",text:"GH"},
    {key:"FH",text:"FH"},
    {key:"PUTTS",text:"PUTTS"},
    {key:"SCORES",text:"SCORES"},
];
var myColumnSet = new YAHOO.widget.ColumnSet(myColumnHeaders);

// Show over 1000 records
var myDataSource = new YAHOO.util.DataSource("/golferswired/LeagueStandings");
// Show 5000 records -- FireBug must be disabled!
//var myDataSource = new YAHOO.util.DataSource("./php/data5000_proxy.php");

// Set the responseType as XML
myDataSource.responseType = YAHOO.util.DataSource.TYPE_XML;

// Define the data schema
myDataSource.responseSchema = {
    resultNode: "Result", // Node name of each result item
    fields: ["ROUNDS","USERNAME","BIRDIES","PARS","BOGIES","DOUBLES","GH","FH","PUTTS","SCORES"] // Field names
};

var initialRequest = "get=all&output=xml";
var oConfigs = {
        caption:"Example: Paginated Over XHR",
        initialRequest:initialRequest,

        pageCurrent: 1,
        rowsPerPage: 100,
        startRecordIndex: 1,
        pageLinksLength: 10,
        rowsPerPageDropdown: [25,50,100,500]
};
var myDataTable = new YAHOO.widget.DataTable("paginated", myColumnSet, myDataSource, oConfigs);
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial