1
4

1 回答 1

1

查看XML writer 的代码,您似乎无法使用标准 XML writer 来实现这一点。您必须扩展或覆盖其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'
        }
    }
于 2013-06-03T16:23:50.620 回答