如何通过castor生成的对象在XML中按属性值查找元素

发布于 2024-11-13 07:33:29 字数 228 浏览 1 评论 0原文

我无法通过 Castor 生成的类在 XML 文件中按属性值查找元素。

Castor 很好地完成了 XML 到实体类的映射。但是,它不提供按名称查找元素的方法(假设名称是元素的属性)。 API 只能通过元素列表中的索引来查找元素。目前看来,如果我想按名称查找元素,我必须循环遍历元素列表才能找到它。因为它解组的每个类都有不同的字段,所以似乎很难编写一个通用方法来按名称查找元素。有没有更好的方法通过名称从元素列表中查找元素?

I am having trouble looking up elements by attribute value in the XML file via castor generated classes.

Castor does the mapping of XML to entity classes very good. However it doesn't provide means to find the element by name (assume that name is an attribute of the element). The API can only find an element by its index in the element list. At the moment it seems if I want to find an element by name I have to loop through the element list to find it. Because every class it unmarshalled has different fields it seems hard to write a generic method to find an element by name. Is there a better way to find an element from the element list by name?

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

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

发布评论

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

评论(1

自控 2024-11-20 07:33:29

注意:我是 EclipseLink JAXB (MOXy) 负责人,以及 JAXB (JSR-222) 专家组成员。


不确定 Castor,但您可以使用 EclipseLink JAXB (MOXy) 轻松完成此操作。

客户

请注意如何通过 @XmlPath 注释在映射上指定条件 [@name='address']

package blog.predicate;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name="node")
@XmlType(propOrder={"firstName", "lastName", "address", "phoneNumbers"})
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {

    @XmlPath("node[@name='first-name']/text()")
    private String firstName;

    @XmlPath("node[@name='last-name']/text()")
    private String lastName;

    @XmlPath("node[@name='address']")
    private Address address;

    @XmlPath("node[@name='phone-number']")
    private List<PhoneNumber> phoneNumbers;

}

XML< /strong>

<?xml version="1.0" encoding="UTF-8"?>
<node>
   <node name="first-name">Jane</node>
   <node name="last-name">Doe</node>
   <node name="address">
      <node name="street">123 A Street</node>
   </node>
   <node name="phone-number" type="work">555-1111</node>
   <node name="phone-number" type="cell">555-2222</node>
</node>

了解更多信息

Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB (JSR-222) expert group.


Not sure about Castor, but you can do this easy enough with EclipseLink JAXB (MOXy).

Customer

Note how a condition [@name='address'] is specified on the mapping via the @XmlPath annotation:

package blog.predicate;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name="node")
@XmlType(propOrder={"firstName", "lastName", "address", "phoneNumbers"})
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {

    @XmlPath("node[@name='first-name']/text()")
    private String firstName;

    @XmlPath("node[@name='last-name']/text()")
    private String lastName;

    @XmlPath("node[@name='address']")
    private Address address;

    @XmlPath("node[@name='phone-number']")
    private List<PhoneNumber> phoneNumbers;

}

XML

<?xml version="1.0" encoding="UTF-8"?>
<node>
   <node name="first-name">Jane</node>
   <node name="last-name">Doe</node>
   <node name="address">
      <node name="street">123 A Street</node>
   </node>
   <node name="phone-number" type="work">555-1111</node>
   <node name="phone-number" type="cell">555-2222</node>
</node>

For More Information

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文