我使用ExtJs创建了这个XML:
<Entity><Fields><Field><value>1777</value></Field><id></id></Fields></Entity>
使用Ext.data.writer.XmlView source。
我的问题是 - 为了将XML属性添加到其中一个字段,我需要做什么? 例如:
&LT;字段 name =“request-id”&gt;
仅供参考,那是我用来在ExtJS中编写XML的代码:
var proxy1 = new Ext.data.proxy.Rest({
url : url,
pageParam: undefined,
limitParam: undefined,
sortParam: undefined,
startParam: undefined,
noCache: false,
appendId: false,
reader : {
type : 'json',
root : 'entities'
},
headers: {
"Content-Type": "application/xml"
},
actionMethods: {
read: "POST"
},
writer: {
writeRecordId: false,
type: 'xml',
nameProperty: 'mapping',
writeAllFields: true,
documentRoot: "Entity",
record: "Fields"
}
});
var model = Ext.define('Entity', {
extend: 'Ext.data.Model',
fields: [
{name: 'Field', type: 'string'}
]
});
var store = Ext.create('Ext.data.Store', {
autoLoad: true,
autoSync: true,
proxy: proxy1,
model: 'Entity',
data : [
{'Field': '<value>1777</value>' }
]
});
答案 0 :(得分:1)
看看the code of the XML writer,看起来你无法用标准的XML编写器来实现这一目标。您必须扩展或覆盖其writeRecord
方法。
例如,您可以这样做:
Ext.define('AttributeAwareXmlWriter', {
extend: 'Ext.data.writer.Xml'
,alias: 'writer.aaxml'
,writeRecords: function (request, data) {
var me = this,
xml = [],
i = 0,
len = data.length,
root = me.documentRoot,
record = me.record,
recordAttributes = me.recordAttributes,
needsRoot = data.length !== 1,
item,
key;
// may not exist
xml.push(me.header || '');
if (!root && needsRoot) {
root = me.defaultDocumentRoot;
}
if (root) {
xml.push('<', root, '>');
}
for (; i < len; ++i) {
item = data[i];
xml.push('<', record);
if (recordAttributes) {
for (key in recordAttributes) {
xml.push(' ', key, '="', recordAttributes[key], '"');
}
}
xml.push('>');
for (key in item) {
if (item.hasOwnProperty(key)) {
xml.push('<', key, '>', item[key], '</', key, '>');
}
}
xml.push('</', record, '>');
}
if (root) {
xml.push('</', root, '>');
}
request.xmlData = xml.join('');
return request;
}
});
包含这样的课程后,您可以使用以下作者:
writer: {
writeRecordId: false,
type: 'aaxml', // changed to custom type
nameProperty: 'mapping',
writeAllFields: true,
documentRoot: "Entity",
record: "Fields",
recordAttributes: {
name: 'request-id'
}
}