13

私は次のようにドキュメントオブジェクトを初期化しています:

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();

その後、doc オブジェクトにデータを挿入して XML ファイルを作成しています。

最後に、コンテンツを自分のコンピューター上のファイルに書き込んでいます。

私の質問は、doc の内容をbyte[]?*に書き込む方法です。

これは、コンテンツを XML ファイルに書き込む方法です。

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("changeOut.xml"));
// Output to console for testing
// StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
4

3 に答える 3

27

Fileの代わりにOutputStreamをコンストラクターに渡します。StreamResult

 ByteArrayOutputStream bos=new ByteArrayOutputStream();
 StreamResult result=new StreamResult(bos);
 transformer.transform(source, result);
 byte []array=bos.toByteArray();
于 2012-08-16T07:49:56.823 に答える
2

ファイルがある場所にByteArrayOutputStreamを配置すると、問題がないはずです。

于 2012-08-16T07:49:18.827 に答える