本文整理汇总了Java中javax.xml.stream.XMLEventWriter.close方法的典型用法代码示例。如果您正苦于以下问题:Java XMLEventWriter.close方法的具体用法?Java XMLEventWriter.close怎么用?Java XMLEventWriter.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.stream.XMLEventWriter
的用法示例。
在下文中一共展示了XMLEventWriter.close方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testXMLStreamWriter
import javax.xml.stream.XMLEventWriter; //导入方法依赖的package包/类
/**
* Test XMLStreamWriter parsing a file with an external entity reference.
*/
@Test
public void testXMLStreamWriter() {
try {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(System.out);
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
String file = getClass().getResource("XMLEventWriterTest.xml").getPath();
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StreamSource(new File(file)));
// adds the event to the consumer.
eventWriter.add(eventReader);
eventWriter.flush();
eventWriter.close();
// expected success
} catch (Exception exception) {
exception.printStackTrace();
Assert.fail(exception.toString());
}
}
示例2: xmlToJson
import javax.xml.stream.XMLEventWriter; //导入方法依赖的package包/类
void xmlToJson(InputStream xmlInput, OutputStream jsonOutput) throws XMLStreamException
{
JsonXMLConfig config = new JsonXMLConfigBuilder()
.autoArray(true)
.autoPrimitive(false)
.fieldPrefix("")
.contentField("content")
.build();
XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(xmlInput);
XMLEventWriter writer = new JsonXMLOutputFactory(config).createXMLEventWriter(jsonOutput);
writer.add(reader);
reader.close();
writer.close();
}
示例3: testCR6419687
import javax.xml.stream.XMLEventWriter; //导入方法依赖的package包/类
/**
* Test: 6419687 NPE in XMLEventWriterImpl.
*/
@Test
public void testCR6419687() {
try {
InputStream in = getClass().getResourceAsStream("ReaderToWriterTest.wsdl");
OutputStream out = new FileOutputStream(USER_DIR + "ReaderToWriterTest-out.xml");
XMLEventReader reader = XML_INPUT_FACTORY.createXMLEventReader(in);
XMLEventWriter writer = XML_OUTPUT_FACTORY.createXMLEventWriter(out, "UTF-8");
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
writer.add(event);
}
reader.close();
writer.close();
} catch (XMLStreamException xmlStreamException) {
xmlStreamException.printStackTrace();
Assert.fail(xmlStreamException.toString());
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
Assert.fail(fileNotFoundException.toString());
}
}
示例4: testCR6419687
import javax.xml.stream.XMLEventWriter; //导入方法依赖的package包/类
/**
* Test: 6419687 NPE in XMLEventWriterImpl.
*/
@Test
public void testCR6419687() {
try {
InputStream in = getClass().getResourceAsStream("ReaderToWriterTest.wsdl");
OutputStream out = new FileOutputStream("ReaderToWriterTest-out.xml");
XMLEventReader reader = XML_INPUT_FACTORY.createXMLEventReader(in);
XMLEventWriter writer = XML_OUTPUT_FACTORY.createXMLEventWriter(out, "UTF-8");
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
writer.add(event);
}
reader.close();
writer.close();
} catch (XMLStreamException xmlStreamException) {
xmlStreamException.printStackTrace();
Assert.fail(xmlStreamException.toString());
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
Assert.fail(fileNotFoundException.toString());
}
}
示例5: writeEvents
import javax.xml.stream.XMLEventWriter; //导入方法依赖的package包/类
private void writeEvents(XMLEventReader reader, XMLEventWriter writer) throws XMLStreamException {
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
writer.add(event);
}
reader.close();
writer.close();
}
示例6: testEventWriterWithStAXResultNStreamWriter
import javax.xml.stream.XMLEventWriter; //导入方法依赖的package包/类
@Test
public void testEventWriterWithStAXResultNStreamWriter() {
String encoding = "";
if (getSystemProperty("file.encoding").equals("UTF-8")) {
encoding = " encoding=\"UTF-8\"";
}
final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"" + encoding + "?><root></root>";
try {
XMLOutputFactory ofac = XMLOutputFactory.newInstance();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
XMLStreamWriter swriter = ofac.createXMLStreamWriter(buffer);
StAXResult res = new StAXResult(swriter);
XMLEventWriter writer = ofac.createXMLEventWriter(res);
XMLEventFactory efac = XMLEventFactory.newInstance();
writer.add(efac.createStartDocument(null, "1.0"));
writer.add(efac.createStartElement("", "", "root"));
writer.add(efac.createEndElement("", "", "root"));
writer.add(efac.createEndDocument());
writer.close();
Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.toString());
}
}
示例7: testEventWriterWithStAXResultNEventWriter
import javax.xml.stream.XMLEventWriter; //导入方法依赖的package包/类
@Test
public void testEventWriterWithStAXResultNEventWriter() {
String encoding = "";
if (getSystemProperty("file.encoding").equals("UTF-8")) {
encoding = " encoding=\"UTF-8\"";
}
final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"" + encoding + "?><root></root>";
try {
XMLOutputFactory ofac = XMLOutputFactory.newInstance();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
XMLEventWriter writer = ofac.createXMLEventWriter(buffer);
StAXResult res = new StAXResult(writer);
writer = ofac.createXMLEventWriter(res);
XMLEventFactory efac = XMLEventFactory.newInstance();
writer.add(efac.createStartDocument(null, "1.0"));
writer.add(efac.createStartElement("", "", "root"));
writer.add(efac.createEndElement("", "", "root"));
writer.add(efac.createEndDocument());
writer.close();
Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.toString());
}
}
示例8: testEventWriterWithStAXResultNStreamWriter
import javax.xml.stream.XMLEventWriter; //导入方法依赖的package包/类
@Test
public void testEventWriterWithStAXResultNStreamWriter() {
String encoding = "";
if (System.getProperty("file.encoding").equals("UTF-8")) {
encoding = " encoding=\"UTF-8\"";
}
final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"" + encoding + "?><root></root>";
try {
XMLOutputFactory ofac = XMLOutputFactory.newInstance();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
XMLStreamWriter swriter = ofac.createXMLStreamWriter(buffer);
StAXResult res = new StAXResult(swriter);
XMLEventWriter writer = ofac.createXMLEventWriter(res);
XMLEventFactory efac = XMLEventFactory.newInstance();
writer.add(efac.createStartDocument(null, "1.0"));
writer.add(efac.createStartElement("", "", "root"));
writer.add(efac.createEndElement("", "", "root"));
writer.add(efac.createEndDocument());
writer.close();
Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.toString());
}
}
示例9: testEventWriterWithStAXResultNEventWriter
import javax.xml.stream.XMLEventWriter; //导入方法依赖的package包/类
@Test
public void testEventWriterWithStAXResultNEventWriter() {
String encoding = "";
if (System.getProperty("file.encoding").equals("UTF-8")) {
encoding = " encoding=\"UTF-8\"";
}
final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"" + encoding + "?><root></root>";
try {
XMLOutputFactory ofac = XMLOutputFactory.newInstance();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
XMLEventWriter writer = ofac.createXMLEventWriter(buffer);
StAXResult res = new StAXResult(writer);
writer = ofac.createXMLEventWriter(res);
XMLEventFactory efac = XMLEventFactory.newInstance();
writer.add(efac.createStartDocument(null, "1.0"));
writer.add(efac.createStartElement("", "", "root"));
writer.add(efac.createEndElement("", "", "root"));
writer.add(efac.createEndDocument());
writer.close();
Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.toString());
}
}
示例10: write
import javax.xml.stream.XMLEventWriter; //导入方法依赖的package包/类
/**
* Writes the processed {@link XMLEvent}s to an {@link OutputStream}.
* @param outputStream The {@link OutputStream} to write the {@link XMLEvent}s to.
* @return The {@link Stockpile} instance for chaining.
* @throws XMLStreamException If an XML error occurs.
*/
public Stockpile write(OutputStream outputStream) throws XMLStreamException {
XMLEventWriter writer = output.createXMLEventWriter(outputStream);
try {
addEventsTo(writer);
} finally {
writer.flush();
writer.close();
}
return this;
}
示例11: testEncodingXmlEventReader
import javax.xml.stream.XMLEventWriter; //导入方法依赖的package包/类
public void testEncodingXmlEventReader() throws Exception {
TEST_XML_WITH_XML_HEADER_ISO_8859_1_AS_BYTE_ARRAY_STREAM.reset();
XMLEventReader reader = null;
XMLEventWriter writer = null;
ByteArrayOutputStream output = null;
try {
// enter text encoded with Latin1
reader = context.getTypeConverter().mandatoryConvertTo(XMLEventReader.class,
TEST_XML_WITH_XML_HEADER_ISO_8859_1_AS_BYTE_ARRAY_STREAM);
output = new ByteArrayOutputStream();
// ensure UTF-8 encoding
Exchange exchange = new DefaultExchange(context);
exchange.setProperty(Exchange.CHARSET_NAME, UTF_8.toString());
writer = context.getTypeConverter().mandatoryConvertTo(XMLEventWriter.class, exchange, output);
while (reader.hasNext()) {
writer.add(reader.nextEvent());
}
} finally {
if (reader != null) {
reader.close();
}
if (writer != null) {
writer.close();
}
}
assertNotNull(output);
String result = new String(output.toByteArray(), UTF_8.name());
// normalize the auotation mark
if (result.indexOf('\'') > 0) {
result = result.replace('\'', '"');
}
boolean equals = TEST_XML_WITH_XML_HEADER.equals(result) || TEST_XML_WITH_XML_HEADER_ISO_8859_1.equals(result);
assertTrue("Should match header", equals);
}
示例12: testMerge
import javax.xml.stream.XMLEventWriter; //导入方法依赖的package包/类
/**
* Inspired by CR 6245284 Sun Stax /sjsxp.jar does not behave properly
* during merge of xml files.
*/
@Test
public void testMerge() {
try {
// Create the XML input factory
XMLInputFactory factory = XMLInputFactory.newInstance();
// Create XML event reader 1
InputStream inputStream1 = new FileInputStream(new File(XMLEventWriterTest.class.getResource("merge-1.xml").toURI()));
XMLEventReader r1 = factory.createXMLEventReader(inputStream1);
// Create XML event reader 2
InputStream inputStream2 = new FileInputStream(new File(XMLEventWriterTest.class.getResource("merge-2.xml").toURI()));
XMLEventReader r2 = factory.createXMLEventReader(inputStream2);
// Create the output factory
XMLOutputFactory xmlof = XMLOutputFactory.newInstance();
// Create XML event writer
XMLEventWriter xmlw = xmlof.createXMLEventWriter(System.out);
// Read to first <product> element in document 1
// and output to result document
QName bName = new QName("b");
while (r1.hasNext()) {
// Read event to be written to result document
XMLEvent event = r1.nextEvent();
if (event.getEventType() == XMLEvent.END_ELEMENT) {
// Start element - stop at <product> element
QName name = event.asEndElement().getName();
if (name.equals(bName)) {
QName zName = new QName("z");
boolean isZr = false;
while (r2.hasNext()) {
// Read event to be written to result document
XMLEvent event2 = r2.nextEvent();
// Output event
if (event2.getEventType() == XMLEvent.START_ELEMENT && event2.asStartElement().getName().equals(zName)) {
isZr = true;
}
if (xmlw != null && isZr) {
xmlw.add(event2);
}
// stop adding events after </z>
// i.e. do not write END_DOCUMENT :)
if (isZr && event2.getEventType() == XMLEvent.END_ELEMENT && event2.asEndElement().getName().equals(zName)) {
isZr = false;
}
}
xmlw.flush();
}
}
// Output event
if (xmlw != null) {
xmlw.add(event);
}
}
// Read to first <product> element in document 1
// without writing to result document
xmlw.close();
// expected success
} catch (Exception ex) {
ex.printStackTrace();
Assert.fail(ex.toString());
}
}