开发者

Mapping XML responses to data records ( Smartgwt)

开发者 https://www.devze.com 2023-04-10 06:29 出处:网络
I have subclassed RestDatasource to create my own data source. This is the constructor to my Datasource

I have subclassed RestDatasource to create my own data source. This is the constructor to my Datasource

    public CustomDS (){
     setDataProtocol(DSProtocol.POSTMESSAGE);
     setDataFormat(DSDataFormat.XML);

     DataSourceTextField firstNameField = new DataSourceTextField("firstName", "First Name");
     DataSourceTextField lastNameField = new DataSourceTextField("lastName", "Last Name");
     DataSourceTextField userIDField = new DataSourceTextField("id", "User ID");

     setFields(firstNameField, lastNameField, userIDField);

     setXmlRecordXPath("/qm:GetResultsResponse/*");
     XmlNamespaces ns = new XmlNamespaces();
     ns.addNamespace("qm", "someurl");
     setXmlNamespaces(ns);

}

This is the xml response

 <?xml version="1.0" encoding="UTF-8"?>
 <qm:GetResultsResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:qm="someurl" xmlns:warehouse="someurl">
   <records xsi:type="warehouse:User" id="id1" firstName="fname1" lastName="Reddy">
     <voiceUserProperties languageId="en-US"/>
    </records>
   <records xsi:type="warehouse:User" id="id2" firstName="fname3" lastName="Reddy">
    <voiceUserProperties languageId="en-US"/>
   </records>
   <records xsi:type="warehouse:User" id="id3" firstName="fnam4" lastName="Reddy">
     <voiceUser开发者_StackOverflow中文版Properties languageId="en-US"/>
   </records>
 </qm:GetResultsResponse>

QUESTION

In the transformResponse() method , response.getDataAsRecordList().getLength() returns 3, But i cant seem to have the records filled out with the required attributes(ie firstName, id , lastName). Does anyone see anything wrong here ?

EDIT: As suggested i changed the datasource to extend from DataSource and not RestDataSource. I still have this problem. If i remove xsi:type="warehouse:User" from the XML , this works fine. Any ideas on this ?


If you're trying to parse a custom format like you've shown, don't subclass RestDataSource, subclass just DataSource. RestDataSource has a lot of settings on it specific to the message format it expects, which has a lot more structure than what you're trying to parse.

.. now that you're using DataSource instead - if you can, get rid of the xsi:type declarations, as they are wasted bytes. However if you grab a nightly build (from smartclient.com/builds) you will see that these declarations are now ignored when processing XML unless the type refers to a particular DataSource that you've declared.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号