Skip to content

Commit 48fdec9

Browse files
committed
Update XmlMapper.java
1 parent 11d5fdc commit 48fdec9

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/main/java/com/fasterxml/jackson/dataformat/xml/XmlMapper.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.fasterxml.jackson.dataformat.xml;
22

3+
import java.io.File;
4+
import java.io.FileOutputStream;
35
import java.io.IOException;
46
import java.io.OutputStream;
57

@@ -9,6 +11,7 @@
911
import javax.xml.stream.XMLStreamWriter;
1012

1113
import com.fasterxml.jackson.core.*;
14+
import com.fasterxml.jackson.core.exc.StreamWriteException;
1215
import com.fasterxml.jackson.core.type.TypeReference;
1316
import com.fasterxml.jackson.core.util.ByteArrayBuilder;
1417
import com.fasterxml.jackson.databind.*;
@@ -398,7 +401,7 @@ public void writeValue(XMLStreamWriter w0, Object value) throws IOException {
398401
/**
399402
* Method that can be used to serialize any Java value as
400403
* a byte array.
401-
404+
*
402405
* @param value value to write as XML bytes
403406
* @param encoding character encoding for the XML output
404407
* @return byte array representing the XML output
@@ -418,10 +421,36 @@ public byte[] writeValueAsBytes(Object value, String encoding) throws JsonProces
418421
}
419422
}
420423

424+
/**
425+
* Method that can be used to serialize any Java value as
426+
* XML output, written to File provided.
427+
*
428+
* @param resultFile
429+
* @param value
430+
* @param encoding
431+
* @throws IOException
432+
* @throws StreamWriteException
433+
* @throws DatabindException
434+
* @since 2.16
435+
*/
436+
public void writeValue(File resultFile, Object value, String encoding)
437+
throws IOException, StreamWriteException, DatabindException
438+
{
439+
_writeValueAndClose(createGenerator(resultFile, encoding), value);
440+
}
441+
421442
private JsonGenerator createGenerator(OutputStream out, String encoding) throws IOException {
422443
this._assertNotNull("out", out);
423444
JsonGenerator g = ((XmlFactory) _jsonFactory).createGenerator(out, encoding);
424445
this._serializationConfig.initialize(g);
425446
return g;
426447
}
448+
449+
private JsonGenerator createGenerator(File outputFile, String encoding) throws IOException {
450+
_assertNotNull("outputFile", outputFile);
451+
JsonGenerator g = ((XmlFactory) _jsonFactory).createGenerator(
452+
new FileOutputStream(outputFile), encoding);
453+
_serializationConfig.initialize(g);
454+
return g;
455+
}
427456
}

0 commit comments

Comments
 (0)