如何在Spicelib / Parsley for Flex

时间:2017-06-23 12:33:39

标签: actionscript-3 flex parsley

我的XML结构如下所示:

<ROOT>
  <P att1="" att2="">
    <KEYWORDS>
      <K name="" />
      <K name="" />
    </KEYWORDS>
  </P>
  <P att1="" att2="">
    <KEYWORDS>
      <K name="" />
    </KEYWORDS>
  </P>
</ROOT>

我想映射P节点和K节点,但不映射KEYWORDS节点。

我有一个P和K节点的课程

package
{
    [XmlMapping(elementName="P", ignoreUnmappedAttributes="true", ignoreUnmappedChildren="true")]
    public class Package
    {
      [ChoiceType("Keyword")]
      public var keywords:Array;
    }
}

package 
{
    [XmlMapping(elementName="K", ignoreUnmappedAttributes="true", ignoreUnmappedChildren="true")]
    public class Keyword 
    {
        public var name:String;
    }
}

然后我有我的映射器:

var mapper:XmlObjectMapper = XmlObjectMappings
    .forUnqualifiedElements()
    .withRootElement(Package)
    .mappedClasses(Keyword)
    .build();

但这总是导致P类的关键字为空。我实现这一点的唯一方法是创建一个名为Keywords的“一次性”类,我将其映射到Package的keywords属性,该类还有一个映射到Keyword类的keywords属性。然后在Package I中使用setter来提取我想要的实际关键字:

//mapping the new Keywords class
var mapper:XmlObjectMapper = XmlObjectMappings
    .forUnqualifiedElements()
    .withRootElement(Package)
    .mappedClasses(Keywords, Keyword)
    .build();

//the new Package class
package 
{
    [XmlMapping(elementName="P", ignoreUnmappedAttributes="true", ignoreUnmappedChildren="true")]
    public class Package 
    {
        private var _keywords:Array;

        [ChoiceType("Keywords")]
        public function set keywords(value:Array):void 
        {
          //there is always only one KEYWORDS node
          _keywords = value[0].keywords
        }

        public function get keywords():Array 
        {
            return _keywords;
        }
    }
}

//the new intermediary Keywords class
package
{
    [XmlMapping(elementName="KEYWORDS", ignoreUnmappedAttributes="true", ignoreUnmappedChildren="true")]
    public class Keywords 
    {
        [ChoiceType("Keyword")]
        public var keywords:Array;
    }
}

0 个答案:

没有答案