Jsonix - 是否可以创建一个集合并在属性声明中声明两个或更多元素?

时间:2017-07-12 13:23:10

标签: collections elements jsonix

预期的XML格式如下:

<c:condition>
    <a:condition>
        <fieldname>fieldName</fieldname>
        <fieldtest>fieldTest</fieldtest>
        <fieldvalues>
        <fieldvalue>fieldValue</fieldvalue>
        </fieldvalues>
    </a:condition>
    <operator>operator</operator>
    <a:condition>
    ...<some>
    </a:condition>
</c:condition>

<a:condition><operator>都需要在collection中,因为两者都可以包含XML请求所需的数量。

已编辑 Type:elements代码如下:

{{
    var MyModule = {
        name: 'MyModule',
        typeInfos: [{
            type: 'classInfo',
            localName: 'ElementsType',
            propertyInfos: [{
                type: 'elements',
                name: 'c:condition',
                wrapperElementName: {
                    localPart: 'condition',
                    namespaceURI: 'http://us.xomplex',
                    prefix: 'c'
                },
                collection: true,
                elementTypeInfos: [{
                    elementName: {
                        localPart: 'condition',
                        namespaceURI: 'http://us.xomplexio',
                        prefix: 'a'
                    },
                        typeInfo: 'MyModule.SubAtomic'
                    }, {
                        elementName: 'operator',
                        typeInfo: 'String'
                    }]
                }]
        },
        {
            type: 'classInfo',
            localName: 'SubAtomic',
            propertyInfos:[{
                type: 'element',
                name: 'fieldName',
                elementName: 'fieldName',
                typeInfo: 'String'
            },
            {
                type: 'element',
                name: 'fieldTest',
                elementName: 'fieldTest',
                typeInfo: 'String'
            }]
        }],
        elementInfos: [{
            elementName: 'root',
            typeInfo: 'MyModule.ElementsType'
        }]
    };
    console.log("creating unmarsaller");
    var context = new Jsonix.Context([MyModule]);
    var unmarshaller = context.createUnmarshaller();
    var unmarshalled = unmarshaller.unmarshalString('<root><c:condition xmlns:c="http://us.xomplex"><a:condition xmlns:a="http://us.xomplexio">one</a:condition><operator>2</operator><a:condition xmlns:a="http://us.xomplexio"><fieldName>unmra</fieldName><fieldTest>Beneed</fieldTest></a:condition><a:condition xmlns:a="http://us.xomplexio">four</a:condition><operator>AND</operator><operator>4</operator></c:condition></root>');
    console.log("unmarshalled");
    console.log(unmarshalled);

    var marshaller = context.createMarshaller();
    var marshalled = marshaller.marshalString({
        name: {
            localPart: 'root'
        },
    //Marshalling - not working....
        value: {
        //'c:condition': ['one', 2, 'unmra', 'four', 10,4]
            'c:condition': [
                 ['Field','Test'],9
            ]
        }
    });
    console.log(marshalled);                
}}

已编辑看起来elements可以满足我的要求。解组也有效。现在我只需要弄清楚如何从Json格式编组它。任何建议将不胜感激

2 个答案:

答案 0 :(得分:0)

不完全符合您的要求。

您可以使用elementselementReferences属性声明一个包含不同元素作为项的集合属性。类似的东西:

{
    type: 'elementRefs',
    name: 'acondition',
    collection: true,
    elementTypeInfos: [{
        elementName: 'condition',
        typeInfo: 'DOXML.Atomicon'
    }, {
        elementName: 'operator',
        typeInfo: 'String'
    }]
}

此属性包含conditionoperator任何数量或订单。但这并不能保证您拥有相同数量的数字,或者始终condition后跟operator。如果这是您想要的,请考虑将conditionoperator对包装到conditionalOperator之类的元素中。

答案 1 :(得分:0)

这是我解决问题的方法:

var MyModule = {
name: 'MyModule',
typeInfos: [{
    type: 'classInfo',
    localName: 'ElementsType',
    propertyInfos: [{
        type: 'elements',
        name: 'c:condition',
        wrapperElementName: {
            localPart: 'condition',
          namespaceURI: 'http://us.xomplex',
            prefix: 'c'
        },
        collection: true,
        elementTypeInfos: [{
            elementName: {
                localPart: 'condition',
              namespaceURI: 'http://us.xomplexio',
                    prefix: 'a'
            },
            typeInfo: 'MyModule.SubAtomic'
        }, {
            elementName: 'operator',
            typeInfo: 'String'
        }]
    }]
},
{
    type: 'classInfo',
    localName: 'SubAtomic',
    propertyInfos:[{
        type: 'element',
        name: 'fieldName',
        elementName: 'fieldName',
        typeInfo: 'String'
    },
    {
        type: 'element',
        name: 'fieldTest',
        elementName: 'fieldTest',
        typeInfo: 'String'
    },
    {
        type: 'element',
        name: 'fieldValues',
        elementName: 'fieldValues',
        typeInfo: 'MyModule.SubSubAtoms'
    }]
  },
{
    type: 'classInfo',
    localName: 'SubSubAtoms',
    propertyInfos:[
       {
            type: 'element',
            name: 'fieldValue',
            collection: true,
            elementName: 'fieldValue',
            typeInfo: 'String'
        }
     ]
 }
],
    elementInfos: [{
        elementName: 'root',
        typeInfo: 'MyModule.ElementsType',
    },
    {
        elementName: 'atoms',
        typeInfo:'MyModule.SubAtomic'
    }
   ]
};
console.log("creating unmarsaller");
var context = new Jsonix.Context([MyModule]);
var unmarshaller = context.createUnmarshaller();
var unmarshalled = unmarshaller.unmarshalString('<root><c:condition xmlns:c="http://us.xomplex"><a:condition xmlns:a="http://us.xomplexio">one</a:condition><operator>2</operator><a:condition xmlns:a="http://us.xomplexio"><fieldName>unmra</fieldName><fieldTest>Beneed</fieldTest></a:condition><a:condition xmlns:a="http://us.xomplexio">four</a:condition><operator>AND</operator><operator>4</operator></c:condition></root>');
console.log("unmarshalled");
console.log(unmarshalled);

var marshaller = context.createMarshaller();
var marshalled = marshaller.marshalString({
    name: {
        localPart: 'root'
    },
    value: {
         'c:condition': [
            "9",    
            { name:     
                {
                    localPart: 'atoms'
                },
              fieldName: "rating",
              fieldTest: "equals",
              fieldValues: {
                  fieldValue: ["563"]
              } ,
              TYPE_NAME: 'MyModule.SubAtomic'
            },
            "AND",
            { name:     
                {
                    localPart: 'atoms'
                },
              fieldName: "price",
              fieldTest: "between",
              fieldValues: {
                  fieldValue: ["150", "300"]
              } ,
              TYPE_NAME: 'MyModule.SubAtomic'
      }] //end of c:condition value
    }
});
console.log(marshalled);

完成!谢谢@lexicore,你的导游给了我很大的帮助!