将 Castor 用于单值类

发布于 2024-09-24 19:32:06 字数 474 浏览 2 评论 0原文

我有一些要摄取的 XML,其设置如下

<project>
    <client>Some client</client>
    <description>some description</description>
</project>

我需要将此数据解组到的对象是项目和客户端。我该如何为此设置脚轮映射?

<class name="Project">
  <field name="description" type="string" />
  <field name="client" type="Client" />
</class>

如果我使用上面的方法,并且我有一个接受字符串的构造函数,我是否还需要为 Client 类进行映射 - 如果是这样,那需要是什么样子?

I have some XML to ingest that is set up like this

<project>
    <client>Some client</client>
    <description>some description</description>
</project>

The Objects I need to unmarshal this data to are Project and Client. How do I set up my castor mapping for this?

<class name="Project">
  <field name="description" type="string" />
  <field name="client" type="Client" />
</class>

If I used the above, and I have a constructor that takes a string, will I need to also doa mapping for the Client class - if so, what will that need to look like?

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

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

发布评论

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

评论(1

那伤。 2024-10-01 19:32:06

Castor 默认设置(使用内省以 getter/setter 方法获取字段列表,并根据类和成员名称生成 XML 标记)很可能会完成您想要的大部分操作。然而,您会发现您可能最终需要对其进行调整,以确保诸如输出中标签的顺序以及精确的格式之类的事情。

您需要添加的重要项目是每个类的 map-to XML 定义(允许将每个 XML 节点解组到正确的类)和 bind-xml每个字段的节点,以允许字段映射到正确的位置。您还要对 client 字段进行一项额外更改 - 您会注意到客户端传入数据中的字段值不显示为 client 节点的子节点但作为 project 节点的子节点。您可以通过指定 container="true" 来实现此目的,这意味着不会映射类,而只会映射其字段。

(未经测试)类似这样,假设您拥有的客户端字符串将进入名为 name 的字段。

<class name="Project">
 <map-to xml="project"/>
 <field name="client" type="Client" container="true"/>
 <field name="description" type="string"><bind-xml name="description"/></field>
</class>
<class name="Client">
 <field name="name" type="string"><bind-xml name="client"/></field>
</class>

It's quite possible that the Castor defaults - using introspection to get the list of fields with getter/setter methods, and generating XML tags based on class and member names, will do most of what you want. However you'll find you'll probably end up needing to tweak it to make sure of things like the order of the tags in the output, and the precise formatting.

The important items you'll need to add are map-to XML definitions for each class (which allows each XML node to be unmarshalled to the right class) and bind-xml nodes for each field, to allow fields to be mapped to the correct location. There's one additional change you'll make for the client field - you'll notice the field value in the incoming data for the client appears not as a child of the client node but as a child of the project node. You can do this by specifying container="true", which means a class won't be mapped, just its fields.

(Untested) something like this, assuming the client string you have is going into a field called name.

<class name="Project">
 <map-to xml="project"/>
 <field name="client" type="Client" container="true"/>
 <field name="description" type="string"><bind-xml name="description"/></field>
</class>
<class name="Client">
 <field name="name" type="string"><bind-xml name="client"/></field>
</class>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文