在Java中,輸出一個Document對象,可以使用以下方法:
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
// Assume doc is the Document object
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamResult;
// Assume doc is the Document object
JAXBContext jaxbContext = JAXBContext.newInstance(Document.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(doc, new StreamResult(System.out));
import org.jdom2.output.XMLOutputter;
// Assume doc is the Document object
XMLOutputter outputter = new XMLOutputter();
outputter.output(doc, System.out);
這些方法可以將一個Document對象輸出為字符串形式,可以根據(jù)具體需求選擇使用哪種方法。