如何根据 JAXB 中元素的存在分配布尔值?

发布于 2024-11-06 08:47:28 字数 721 浏览 0 评论 0 原文

这与这个问题这个问题我昨天问。

我想使用布尔值来确定 XML 文档中是否存在某个元素。我正在解析的文件允许使用如下元素:

<FamilyMember>
    <Name>Jeff</Name>
</FamilyMember>
<FamilyMember>
    <Name>Spot</Name>
    <IsPet/>
</FamilyMember>

在此示例中,该元素指定 FamilyMember 是宠物,但没有与该元素关联的其他数据。我希望能够告诉 JAXB 根​​据解析文件中是否存在该元素返回一个布尔值。如果该元素存在,则该值应该为 true;否则,它应该是假的。如果可能的话,我想从我用来生成 Java 类的 XSD 模式中实现这一点。

This is somewhat related to this question and this question which I asked yesterday.

I would like to use a boolean to determine whether or not an element exists in an XML document. The files that I am parsing allow for elements such as the following:

<FamilyMember>
    <Name>Jeff</Name>
</FamilyMember>
<FamilyMember>
    <Name>Spot</Name>
    <IsPet/>
</FamilyMember>

In this example, the element specifies that the FamilyMember is a pet, but there is no additional data associated with this element. I would like to be able tell JAXB to return a boolean based on whether or not the element exists in the parsed file. If the element exists, the value should be true; otherwise, it should be false. I would like to to this from within the XSD schema which I use to generate my Java classes, if possible.

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

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

发布评论

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

评论(2

记忆里有你的影子 2024-11-13 08:47:28

您应该能够使用类似于以下内容的 XmlAdapter 来执行此操作:

一旦你得到了答案(如何指定适配器)哪个 JAXB 用于封送/解封数据?)您将能够应用该适配器。


以下是如何做到这一点。请注意,以下示例使用 EclipseLink JAXB (MOXy) 运行,但在以下情况下抛出异常:使用 JAXB 参考实现。

FamilyMember

package example;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement(name="FamilyMember")
public class FamilyMember {

    private boolean pet;
    private String name;

    @XmlElementRef
    @XmlJavaTypeAdapter(IsPetAdapter.class)
    public boolean isPet() {
        return pet;
    }

    public void setPet(boolean pet) {
        this.pet = pet;
    }

    @XmlElement(name="Name")
    public String getName() {
        return name;
    }

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

}

IsPetAdapter

package example;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import forum204.IsPetAdapter.IsPet;

public class IsPetAdapter extends XmlAdapter<IsPet, Boolean> {

    @Override
    public Boolean unmarshal(IsPet v) throws Exception {
        return null != v;
    }

    @Override
    public IsPet marshal(Boolean v) throws Exception {
        if(v) {
            return new IsPet();
        }
        return null;
    }

    @XmlRootElement(name="IsPet")
    public static class IsPet {
    }

}

演示

package example;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(FamilyMember.class);

        Unmarshaller unmarshaller= jc.createUnmarshaller();
        FamilyMember fm = (FamilyMember) unmarshaller.unmarshal(new File("input.xml"));

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(fm, System.out);
    }

}

You should be able to do this with an XmlAdapter similar to the following:

Once you have the answer to ( How do I specify the adapter(s) which JAXB uses for marshaling/unmarshaling data?) you will be able to apply the adapter.


The following is how it could be done. Note the following example works using EclipseLink JAXB (MOXy), but throws an exception when the JAXB reference implementation is used.

FamilyMember

package example;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement(name="FamilyMember")
public class FamilyMember {

    private boolean pet;
    private String name;

    @XmlElementRef
    @XmlJavaTypeAdapter(IsPetAdapter.class)
    public boolean isPet() {
        return pet;
    }

    public void setPet(boolean pet) {
        this.pet = pet;
    }

    @XmlElement(name="Name")
    public String getName() {
        return name;
    }

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

}

IsPetAdapter

package example;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import forum204.IsPetAdapter.IsPet;

public class IsPetAdapter extends XmlAdapter<IsPet, Boolean> {

    @Override
    public Boolean unmarshal(IsPet v) throws Exception {
        return null != v;
    }

    @Override
    public IsPet marshal(Boolean v) throws Exception {
        if(v) {
            return new IsPet();
        }
        return null;
    }

    @XmlRootElement(name="IsPet")
    public static class IsPet {
    }

}

Demo

package example;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(FamilyMember.class);

        Unmarshaller unmarshaller= jc.createUnmarshaller();
        FamilyMember fm = (FamilyMember) unmarshaller.unmarshal(new File("input.xml"));

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(fm, System.out);
    }

}
少年亿悲伤 2024-11-13 08:47:28

您的 IsPet 是否表现为布尔值?在这种情况下,我猜测如果 XML 中不存在布尔值,则该布尔值将为 null。我认为这个问题表明情况确实如此。

Does your IsPet manifest as a Boolean? In which I case I'd guess that the Boolean would be null if it's not present in the XML. I think this questionindicates this is the case.

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