3

我需要将 JavaBeans 编组到 org.w3c.dom.Element
我可以编组到输出流,但不能编组到 XML 文档。
我写了以下测试代码:

JAXBContext context = JAXBContext.newInstance(String.class);            
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

String xmlRootName = "rootTag";
JAXBElement jaxbElement = new JAXBElement(new QName("", xmlRootName), String.class, "foo bar");
Element element;

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();

m.marshal(jaxbElement, doc);
System.out.println("Document: " + doc);

element = doc.createElement(xmlRootName);
m.marshal(jaxbElement, element);
System.out.println("Element1: " + element);

DOMResult res = new DOMResult();
m.marshal(jaxbElement, res);
element = ((Document)res.getNode()).getDocumentElement();
System.out.println("Element2: " + element);

StringWriter writer = new StringWriter();
m.marshal(jaxbElement, writer);
System.out.println("Writer: " + writer);

输出是:

Document: [#document: null]
Element1: [rootTag: null]
Element2: [rootTag: null]
Writer: <rootTag>foo bar</rootTag>

看起来编组仅适用于输出流。如何编组到 xml 文档?
我究竟做错了什么?

4

1 回答 1

2

您正在正确编组对象。要记录结果,您需要使用javax.xml.transformAPI。您得到System.out.println的结果只是在toString可以返回任何内容的 DOM 节点上调用的结果。

Java 模型

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public 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.*;

public class Demo {

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

        // Build the Domain Objects
        Foo foo = new Foo();
        foo.setBar("Hello World");

        // Create the Document
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.newDocument();

        // Marshal Object to the Document
        Marshaller marshaller = jc.createMarshaller();
        marshaller.marshal(foo, document);

        // Perform Operation on Resulting Document
        NodeList elements = document.getElementsByTagName("bar");
        for(int x=0; x<elements.getLength(); x++) {
            System.out.println(elements.item(x).getTextContent());
        }

        // Output Document to System.out
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        t.transform(new DOMSource(document), new StreamResult(System.out));
    }

}

输出

Hello World
<?xml version="1.0" encoding="UTF-8" standalone="no"?><foo><bar>Hello World</bar></foo>
于 2013-09-17T11:59:47.403 回答