Castor - 解组一个简单的重复组
我会慢慢地因为这件事而生气。我正在尝试像这样解组一个 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 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经成功地通过以下映射取得了一些成功:
诀窍是在几个 location="parameters" 和 node="text" 上使用 location="parameters" 和 node="text" >bind-xml 元素。我已经使用此映射成功地编组和解组。
I've managed to have some success with the following mapping:
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.
您对尝试读取的 XML 格式有任何控制权吗?还是必须采用这种格式?
我能够以这种格式写出,但 Castor 不会读回它并引发异常。
我能得到的最接近成功编组和解组的位置是:
Do you have any control over the XML format you are attempting to read or does it have to be in this format?
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: