哪个 JAXB 实现实现了 Marshaller.getNode()?

发布于 2024-11-14 04:56:22 字数 1215 浏览 8 评论 0原文

我正在尝试添加评论< /a> 到 JAXB 生成的 XML @GET 结果,这似乎不是一个直接的任务。我正在使用 Spring,并且 我无法直接访问编组和DOM

通过添加 public void beforeMarshal(Marshaller m) 到我的 @XmlElement,我应该能够使用 Marshaller#getNode(Object),从而能够添加注释。

问题是 AbstractMarshallerImpl#getNode(Object)

默认情况下,getNode方法是 不受支持并抛出一个 java.lang.UnsupportedOperationException。 选择支持的实现 该方法必须重写该方法。

getNode(Object) 是否由任何 JAXB 实现实现?

I am trying to add a comment to a JAXB generated XML @GET result, which doesn't seem to be a straight forward task. I'm using Spring, and I don't have direct access to the marshalling and DOM.

By adding public void beforeMarshal(Marshaller m) to my @XmlElement, I should be able to access the DOM with Marshaller#getNode(Object), and thus be able to add a comment.

The problem is AbstractMarshallerImpl#getNode(Object):

By default, the getNode method is
unsupported and throw an
java.lang.UnsupportedOperationException.
Implementations that choose to support
this method must override this method.

Is getNode(Object) implemented by any JAXB implementations out there?

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

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

发布评论

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

评论(1

风启觞 2024-11-21 04:56:22

注意: 我领导 EclipseLink JAXB (MOXy) 并且是JAXB 2 (JSR-222) 专家组成员。


MOXy 目前不支持可选的 getNode方法参见(增强请求https:// bugs.eclipse.org/332762)。但是,JAXB 的 Binder 可能对以下用例有所帮助:

演示

import javax.xml.bind.Binder;
import javax.xml.bind.JAXBContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

public class Demo {

    public static void main(String[] args) throws Exception {
        Customer customer = new Customer();
        customer.setName("Jane Doe");

        PhoneNumber homePhoneNumber = new PhoneNumber();
        customer.getPhoneNumbers().add(homePhoneNumber);

        PhoneNumber workPhoneNumber = new PhoneNumber();
        customer.getPhoneNumbers().add(workPhoneNumber);

        JAXBContext jc = JAXBContext.newInstance(Customer.class);

        Binder<Node> binder = jc.createBinder();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.newDocument();

        binder.marshal(customer, document);
        Node homePhoneNumberElement = binder.getXMLNode(homePhoneNumber);
        Comment comment = document.createComment("My Comment");
        homePhoneNumberElement.appendChild(comment);

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(System.out);
        t.transform(source, result);
    }

}

客户

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Customer {

    private String name;

    private List<PhoneNumber> phoneNumbers = new ArrayList<PhoneNumber>();

    public String getName() {
        return name;
    }

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

    @XmlElement(name="phone-number")
    public List<PhoneNumber> getPhoneNumbers() {
        return phoneNumbers;
    }

    public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
        this.phoneNumbers = phoneNumbers;
    }

}

电话号码

public class PhoneNumber {

}

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


MOXy does not currently support the optional getNode method see (enhancement request https://bugs.eclipse.org/332762). However, JAXB's Binder may be helpful with this use case:

Demo

import javax.xml.bind.Binder;
import javax.xml.bind.JAXBContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

public class Demo {

    public static void main(String[] args) throws Exception {
        Customer customer = new Customer();
        customer.setName("Jane Doe");

        PhoneNumber homePhoneNumber = new PhoneNumber();
        customer.getPhoneNumbers().add(homePhoneNumber);

        PhoneNumber workPhoneNumber = new PhoneNumber();
        customer.getPhoneNumbers().add(workPhoneNumber);

        JAXBContext jc = JAXBContext.newInstance(Customer.class);

        Binder<Node> binder = jc.createBinder();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.newDocument();

        binder.marshal(customer, document);
        Node homePhoneNumberElement = binder.getXMLNode(homePhoneNumber);
        Comment comment = document.createComment("My Comment");
        homePhoneNumberElement.appendChild(comment);

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(System.out);
        t.transform(source, result);
    }

}

Customer

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Customer {

    private String name;

    private List<PhoneNumber> phoneNumbers = new ArrayList<PhoneNumber>();

    public String getName() {
        return name;
    }

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

    @XmlElement(name="phone-number")
    public List<PhoneNumber> getPhoneNumbers() {
        return phoneNumbers;
    }

    public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
        this.phoneNumbers = phoneNumbers;
    }

}

PhoneNumber

public class PhoneNumber {

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