栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何将JAXB对象编组到org.w3c.dom.Document?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何将JAXB对象编组到org.w3c.dom.Document?

你在做什么应该工作。这是一个例子:

领域模型(Foo)

import javax.xml.bind.annotation.XmlRootElement;@XmlRootElementpublic class Foo {    private String bar;    public String getBar() {        return bar;    }    public void setBar(String bar) {        this.bar = bar;    }}

演示版

import javax.xml.bind.*;import javax.xml.parsers.*;import javax.xml.transform.*;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import org.w3c.dom.document;public class Demo {    public static void main(String[] args) throws Exception {        // Create the JAXBContext        JAXBContext jc = JAXBContext.newInstance(Foo.class);        // Create the Object        Foo foo = new Foo();        foo.setBar("Hello World");        // Create the document        documentBuilderFactory dbf = documentBuilderFactory.newInstance();        documentBuilder db = dbf.newdocumentBuilder();        document document = db.newdocument();        // Marshal the Object to a document        Marshaller marshaller = jc.createMarshaller();        marshaller.marshal(foo, document);        // Output the document        TransformerFactory tf = TransformerFactory.newInstance();        Transformer t = tf.newTransformer();        DOMSource source = new DOMSource(document);        StreamResult result = new StreamResult(System.out);        t.transform(source, result);    }}

输出量

<?xml version="1.0" encoding="UTF-8" standalone="no"?><foo><bar>Hello World</bar></foo>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/463675.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号