将 XML 响应映射到数据记录 (Smartgwt)
我将 RestDatasource 子类化以创建我自己的数据源。这是我的数据源的构造函数
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);
}
这是xml响应
<?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">
<voiceUserProperties languageId="en-US"/>
</records>
</qm:GetResultsResponse>
问题
在transformResponse()方法中,response.getDataAsRecordList().getLength()返回3,但我似乎无法填写记录具有所需的属性(即firstName、id、lastName)。有人看到这里有什么问题吗?
编辑: 按照建议,我将数据源更改为从 DataSource 扩展,而不是 RestDataSource。我仍然有这个问题。 如果我从 XML 中删除 xsi:type="warehouse:User" ,则效果很好。对此有什么想法吗?
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">
<voiceUserProperties 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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您尝试解析像您所显示的那样的自定义格式,请不要对 RestDataSource 进行子类化,而只需对 DataSource 进行子类化。 RestDataSource 有很多特定于它期望的消息格式的设置,其结构比您尝试解析的结构要多得多。
.. 现在您正在使用 DataSource - 如果可以的话,请摆脱 xsi:type 声明,因为它们是浪费的字节。但是,如果您获取夜间构建(来自 smartclient.com/builds),您将看到处理 XML 时这些声明现在被忽略,除非该类型引用您已声明的特定数据源。
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.