【问题标题】:JibX: how to map a class and avoid it being outputed as XML nodeJibX:如何映射一个类并避免它被输出为 XML 节点
【发布时间】:2010-10-10 06:56:22
【问题描述】:

我正在使用JibX 作为 Java 对象到 XML 绑定工具。

有了它,我想得到以下输出:

<?xml version="1.0" encoding="UTF-8"?>
<FEAPService>
    <Request>
        <Function>aaa</Function>
        <SubFunction>bbb</SubFunction>
        <Operation>ccc</Operation>
    </Request>
</FEAPService>

但我明白了:

<?xml version="1.0" encoding="UTF-8"?>
<FEAPService>
    <Request>
        <baseForm> <!-- I DO NOT WANT THIS baseForm TAG -->
            <Function>aaa</Function>
            <SubFunction>bbb</SubFunction>
            <Operation>ccc</Operation>
        </baseForm>
    </Request>
</FEAPService>

这是 JibX 绑定文件:

<binding name="requestBinding_com_struts_form_SpecificForm">
    <mapping name="baseForm" class="com.struts.form.BaseForm">
        <value name="Function" field="function" />
        <value name="SubFunction" field="subFunction" />
        <value name="Operation" field="operation" />
    </mapping>    
    <mapping name="FEAPService" class="com.struts.form.SpecificForm"
        extends="com.struts.form.BaseForm">
        <structure name="Request">
            <structure map-as="com.struts.form.BaseForm" />
        </structure>
    </mapping>
</binding>

我想这可以通过实现我自己的 Marshaller 来实现,但我不确定这是否是最简单的方法。

【问题讨论】:

    标签: java xml data-binding binding


    【解决方案1】:

    基础(抽象)没有名称也很重要。

    【讨论】:

      【解决方案2】:

      我设法删除了不需要的 XML 节点,有两种方法:

      1. 简单的方法:只需使用 abstract="true" 属性。
      2. 硬核方式:实现您自己的编组器。

      第一个选项:

      <mapping class="com.struts.form.BaseForm" abstract="true">
              <value name="Function" field="function" />
              <value name="SubFunction" field="subFunction" />
              <value name="Operation" field="operation" />
              <value name="doubleField_part1" field="doubleField" serializer="com.struts.form.BaseForm.doubleFieldDeserializer1" />
              <value name="doubleField_part2" field="doubleField" serializer="com.struts.form.BaseForm.doubleFieldDeserializer2" />
          </mapping>
      

      第二个选项:实现你自己的 Marshaller

      public class MyMarshaller implements IMarshaller {
          private static final String FUNCTION_ELEMENT_NAME = "Function";
          private static final String SUB_FUNCTION_ELEMENT_NAME = "SubFunction";
          private static final String OPERATION_ELEMENT_NAME = "Operation";
      
          private String m_uri;
          private int m_index;
          private String m_name;
      
          public MyMarshaller() {
              m_uri = null;
              m_index = 0;
      
              m_name = "Function";
          }
      
          public MyMarshaller(String uri, int index, String name) {
              m_uri = uri;
              m_index = index;
              m_name = name;
          }
      
          public boolean isExtension(int index) {
              return false;
          }
      
          public void marshal(Object obj, IMarshallingContext ictx)
                  throws JiBXException {
      
              // make sure the parameters are as expected
              if (!(obj instanceof BaseForm)) {
                  throw new JiBXException("Invalid object type for marshaller");
              } else if (!(ictx instanceof MarshallingContext)) {
                  throw new JiBXException("Invalid object type for marshaller");
              } else {
                  // start by generating start tag for container
                  MarshallingContext ctx = (MarshallingContext) ictx;
                  BaseForm formBean = (BaseForm) obj;
      
                  /*
                   * THIS CODE COULD BE REPEATED FOR ALL FormBean CLASS' ATTRIBUTE USING REFLECTION.
                   * And to control which attributes should be marshalled the attributes could follow special nomeclatures.
                   * Ex.
                   * class FormBean { 
                   *    String marshallable_Function;
                   *    String non_marshallable_Function;
                   * }
                   */ 
                  writeTag(ctx, formBean.getFunction(), FUNCTION_ELEMENT_NAME);
                  writeTag(ctx, formBean.getSubFunction(), SUB_FUNCTION_ELEMENT_NAME);
                  writeTag(ctx, formBean.getOperation(), OPERATION_ELEMENT_NAME);
              }
          }
      
          private void writeTag(MarshallingContext ctx, String value, String entryElementName)
                  throws JiBXException {
              ctx.startTag(m_index, entryElementName);
              ctx.closeStartContent();
              ctx.content(value);
              ctx.endTag(m_index, entryElementName);
          }
      }
      

      这是我的 binding.xml

      <binding name="requestBinding_com_struts_form_SpecificForm">
          <mapping class="com.struts.form.BaseForm" marshaller="example8.MyMarshaller" />        
          <mapping name="FEAPService" class="com.struts.form.SpecificForm">
              <structure name="Request">
                  <structure map-as="com.struts.form.BaseForm" />
              </structure>
          </mapping>
      </binding>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-10
        • 1970-01-01
        • 1970-01-01
        • 2012-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多