Castor - 解组一个简单的重复组

发布于 2024-12-20 00:34:18 字数 2614 浏览 2 评论 0原文

我会慢慢地因为这件事而生气。我正在尝试像这样解组一个 XML 文档:

<GetDeadlineOffset>
    <deadlineCode>DC1</deadlineCode>
    <parameters>
      <parameter name="P1">Param 1</parameter>
      <parameter name="P2">Param 2</parameter>
    </parameters>
</GetDeadlineOffset>

我有一对简单的 POJO、GetDeadlineOffsetRequest 和 Parameter,它们看起来像这样:

public class GetDeadlineOffsetRequest {

    private String deadlineCode = null;
    private List<Parameter> parmList = new ArrayList<Parameter>();


    public GetDeadlineOffsetRequest() {
        // Do nothing
    }

    public String getDeadlineCode(){
        return this.deadlineCode;
    }

    public void setDeadlineCode(String deadlineCode){
        this.deadlineCode = deadlineCode;
    }

    public List<Parameter> getParameters() {
        return parmList;
    }

    public void setParameters(List<Parameter> parmList) {
        this.parmList = parmList;
    }

} 

public class Parameter {

    private String name = null;
    private String value = null;

    public Parameter() {
        // Do Nothing
    }

    public String getName() {
        return name;
    }

    public String getValue() {
        return value;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

正在使用映射文件,但似乎无法将这些标签解组到我的参数中列表。

这是我对映射的最新尝试:

<class name="mypkg.GetDeadlineOffsetRequest">
    <map-to xml="GetDeadlineOffset"/>
    <field name="DeadlineCode" type="java.lang.String">
        <bind-xml name="deadlineCode" node="element" />
    </field>
    <field name="Parameters" type="mypkg.Parameter" collection="collection">
        <bind-xml name="parameters" node="element" />
    </field>    
</class>
<class name="mypkg.Parameter">
    <map-to ns-uri="http://services.blah.com/AMM/Deadline/v1"/>
    <field name="Value" type="java.lang.String">
        <bind-xml name="parameter" node="element" /> 
    </field>
    <field name="Name" type="java.lang.String">
        <bind-xml name="name" node="attribute" location="parameter" />  
    </field>
</class>

问题似乎围绕着以下事实:是容器元素,是是可重复的元素。上面的映射告诉 Castor是可重复的。

这几天我一直在尝试各种映射组合,我想我已经走到了死角!

谁能看到我做错了什么吗?

我正在使用 Castor XML 1.3.2。

I'm going slowly mad over this. I'm trying to unmarshall an XML document like this:

<GetDeadlineOffset>
    <deadlineCode>DC1</deadlineCode>
    <parameters>
      <parameter name="P1">Param 1</parameter>
      <parameter name="P2">Param 2</parameter>
    </parameters>
</GetDeadlineOffset>

I have a pair of simple POJOs, GetDeadlineOffsetRequest and Parameter, which look like this:

public class GetDeadlineOffsetRequest {

    private String deadlineCode = null;
    private List<Parameter> parmList = new ArrayList<Parameter>();


    public GetDeadlineOffsetRequest() {
        // Do nothing
    }

    public String getDeadlineCode(){
        return this.deadlineCode;
    }

    public void setDeadlineCode(String deadlineCode){
        this.deadlineCode = deadlineCode;
    }

    public List<Parameter> getParameters() {
        return parmList;
    }

    public void setParameters(List<Parameter> parmList) {
        this.parmList = parmList;
    }

} 

and

public class Parameter {

    private String name = null;
    private String value = null;

    public Parameter() {
        // Do Nothing
    }

    public String getName() {
        return name;
    }

    public String getValue() {
        return value;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

I'm using a mapping file but can't seem to get those tags unmarshalled into my Parameter list.

This is my latest attempt at a mapping:

<class name="mypkg.GetDeadlineOffsetRequest">
    <map-to xml="GetDeadlineOffset"/>
    <field name="DeadlineCode" type="java.lang.String">
        <bind-xml name="deadlineCode" node="element" />
    </field>
    <field name="Parameters" type="mypkg.Parameter" collection="collection">
        <bind-xml name="parameters" node="element" />
    </field>    
</class>
<class name="mypkg.Parameter">
    <map-to ns-uri="http://services.blah.com/AMM/Deadline/v1"/>
    <field name="Value" type="java.lang.String">
        <bind-xml name="parameter" node="element" /> 
    </field>
    <field name="Name" type="java.lang.String">
        <bind-xml name="name" node="attribute" location="parameter" />  
    </field>
</class>

The problem seems to revolve around the fact that <parameters> is the container element and <parameter> is the repeatable element. The mapping above is telling Castor that <parameters> is repeatable.

I've been trying all sorts of combinations of mappings for a couple of days now and I think I've worked my way into a corner!

Can anyone see what I'm doing wrong?

I'm using Castor XML 1.3.2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黒涩兲箜 2024-12-27 00:34:18

我已经成功地通过以下映射取得了一些成功:

<class name="myPkg.GetDeadlineOffsetRequest">
    <map-to xml="GetDeadlineOffset"/>
    <field name="DeadlineCode">
        <bind-xml name="deadlineCode" />
    </field>
    <field name="parameters" type="myPkg.Parameter" collection="collection">
        <bind-xml name="parameter" location="parameters">
            <class name="myPkg.Parameter">
                <field name="Name">
                    <bind-xml name="name" node="attribute" />
                </field>
                <field name="Value">
                    <bind-xml node="text"/>
                </field>
            </class>
        </bind-xml>
    </field>        
</class>

诀窍是在几个 location="parameters"node="text" 上使用 location="parameters"node="text" >bind-xml 元素。我已经使用此映射成功地编组和解组。

I've managed to have some success with the following mapping:

<class name="myPkg.GetDeadlineOffsetRequest">
    <map-to xml="GetDeadlineOffset"/>
    <field name="DeadlineCode">
        <bind-xml name="deadlineCode" />
    </field>
    <field name="parameters" type="myPkg.Parameter" collection="collection">
        <bind-xml name="parameter" location="parameters">
            <class name="myPkg.Parameter">
                <field name="Name">
                    <bind-xml name="name" node="attribute" />
                </field>
                <field name="Value">
                    <bind-xml node="text"/>
                </field>
            </class>
        </bind-xml>
    </field>        
</class>

The trick was to use location="parameters" and node="text" on a couple of the bind-xml elements. I've successfully marshalled and unmarshalled using this mapping.

独木成林 2024-12-27 00:34:18

您对尝试读取的 XML 格式有任何控制权吗?还是必须采用这种格式?

<GetDeadlineOffset>
    <deadlineCode>DC1</deadlineCode>
    <parameters>
      <parameter name="P1">Param 1</parameter>
      <parameter name="P2">Param 2</parameter>
    </parameters>
</GetDeadlineOffset>

我能够以这种格式写出,但 Castor 不会读回它并引发异常。

我能得到的最接近成功编组和解组的位置是:

<GetDeadlineOffset>
    <deadlineCode>DeadlineCode</deadlineCode>
    <parameters>
        <parameter name="Name1" value="Value1"/>
        <parameter name="Name2" value="Value2"/>
    </parameters>
</GetDeadlineOffset>

Do you have any control over the XML format you are attempting to read or does it have to be in this format?

<GetDeadlineOffset>
    <deadlineCode>DC1</deadlineCode>
    <parameters>
      <parameter name="P1">Param 1</parameter>
      <parameter name="P2">Param 2</parameter>
    </parameters>
</GetDeadlineOffset>

I was able to get this format to write out but Castor would not read it back in throws an exception.

The closest I could get where it would successfully marshal and unmarshal is:

<GetDeadlineOffset>
    <deadlineCode>DeadlineCode</deadlineCode>
    <parameters>
        <parameter name="Name1" value="Value1"/>
        <parameter name="Name2" value="Value2"/>
    </parameters>
</GetDeadlineOffset>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文