2017-07-12 25 views
0

預期XML格式是按照下文:Jsonix - 是否可以在屬性聲明中創建一個集合並聲明2個或多個元素?

<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);     
}} 

EDITED貌似elements能滿足我的要求。解組也起作用。現在我只需要弄清楚如何從Json格式編組它。任何意見將不勝感激

回答

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,你的導遊是一個很好的幫助!

0

不完全按照你想要的。

您可以使用elementselementReferences屬性來聲明一個集合屬性,該集合屬性將包含與項目不同的元素。喜歡的東西:

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

此屬性將包含conditionoperator INS任何數量或順序。但是這並不能保證你有相同的數量,或者總是condition,然後是operator。如果這是您想要的,請考慮將conditionoperator對包裝到像conditionalOperator這樣的元素中。

+0

謝謝。你的導遊很棒!我決定使用'元素'。我編輯了我的問題。現在,我無法使用正確的Json格式將Json編組爲XML。 – codingKit

+0

這是另一個問題。 – lexicore

相關問題