本文整理汇总了Java中javax.xml.stream.XMLEventWriter.flush方法的典型用法代码示例。如果您正苦于以下问题:Java XMLEventWriter.flush方法的具体用法?Java XMLEventWriter.flush怎么用?Java XMLEventWriter.flush使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.stream.XMLEventWriter
的用法示例。
在下文中一共展示了XMLEventWriter.flush方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: testCreateStartDocument_DOMWriter
import javax.xml.stream.XMLEventWriter; //导入方法依赖的package包/类
/**
* @bug 8139584
* Verifies that the resulting XML contains the standalone setting.
*/
@Test
public void testCreateStartDocument_DOMWriter()
throws ParserConfigurationException, XMLStreamException {
XMLOutputFactory xof = XMLOutputFactory.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
XMLEventWriter eventWriter = xof.createXMLEventWriter(new DOMResult(doc));
XMLEventFactory eventFactory = XMLEventFactory.newInstance();
XMLEvent event = eventFactory.createStartDocument("iso-8859-15", "1.0", true);
eventWriter.add(event);
eventWriter.flush();
Assert.assertEquals(doc.getXmlEncoding(), "iso-8859-15");
Assert.assertEquals(doc.getXmlVersion(), "1.0");
Assert.assertTrue(doc.getXmlStandalone());
}
示例3: testCreateStartDocument
import javax.xml.stream.XMLEventWriter; //导入方法依赖的package包/类
/**
* @bug 8139584
* Verifies that the resulting XML contains the standalone setting.
*/
@Test
public void testCreateStartDocument() throws XMLStreamException {
StringWriter stringWriter = new StringWriter();
XMLOutputFactory out = XMLOutputFactory.newInstance();
XMLEventFactory eventFactory = XMLEventFactory.newInstance();
XMLEventWriter eventWriter = out.createXMLEventWriter(stringWriter);
XMLEvent event = eventFactory.createStartDocument("iso-8859-15", "1.0", true);
eventWriter.add(event);
eventWriter.flush();
Assert.assertTrue(stringWriter.toString().contains("encoding=\"iso-8859-15\""));
Assert.assertTrue(stringWriter.toString().contains("version=\"1.0\""));
Assert.assertTrue(stringWriter.toString().contains("standalone=\"yes\""));
}
示例4: 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;
}
示例5: 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());
}
}