gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:48:27 25 4
gpt4 key购买 nike

我正在尝试 add a comment到 JAXB 生成的 XML @GET 结果,这似乎不是一项直接的任务。我正在使用 Spring,并且 I don't have direct access to the marshalling and DOM .

通过添加 public void beforeMarshal(Marshaller m)对于我的@XmlElement,我应该能够使用 Marshaller#getNode(Object) 访问 DOM , 从而能够添加评论。

问题是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.

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

最佳答案

注:我领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 {

}

关于java - 哪个 JAXB 实现实现了 Marshaller.getNode()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6251232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com