gpt4 book ai didi

jaxb 编码到 XML 文档

转载 作者:行者123 更新时间:2023-12-02 01:57:57 25 4
gpt4 key购买 nike

我需要将 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文件?
我做错了什么?

最佳答案

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

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>

关于jaxb 编码到 XML 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18848453/

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