Добавьте XML-комментарии в упорядоченный файл

Большая часть стандартного математического использования библиотеки удваивает, но обеспечивает версии плавающие также. станд.:: floorf () является версией одинарной точности станд.:: пол (), если Вы предпочли бы не использовать, удваивается.

Редактирование: я удалил часть своего предыдущего ответа. Я заявил, что пол был избыточен при кастинге к интервалу, но я забыл, что это только верно для положительных чисел с плавающей точкой.

16
задан beta0x64 1 February 2012 в 20:49
поделиться

1 ответ

I do not see a way to do it with JAXB alone. However, I think you can leverage DOM to get the desired effect:

final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document doc = builder.getDOMImplementation().createDocument(null, null, null);

final Binder<Node> binder = jaxbContext.createBinder();
binder.marshal(jaxbObject, doc);
final Comment comment = doc.createComment("This is a comment");
doc.appendChild(comment);

final DOMSource domSource = new DOMSource(doc);
// use System.out for testing
final StreamResult streamResult = new StreamResult(System.out);
final TransformerFactory tf = TransformerFactory.newInstance();
final Transformer serializer = tf.newTransformer();
serializer.transform(domSource, streamResult);

Where jaxbContext is the JAXBContext object you are working with and jaxbObject is the object to be marshalled. This sample just appends the comment to the end of the document. For a different location, you would have to traverse the DOM through the doc object or use XPath to find the exact element you want the comment added to and use appendChild on it.

7
ответ дан 30 November 2019 в 22:02
поделиться
Другие вопросы по тегам:

Похожие вопросы: