當前位置: 首頁>>代碼示例>>Java>>正文


Java XMLOutputFactory.createXMLEventWriter方法代碼示例

本文整理匯總了Java中javax.xml.stream.XMLOutputFactory.createXMLEventWriter方法的典型用法代碼示例。如果您正苦於以下問題:Java XMLOutputFactory.createXMLEventWriter方法的具體用法?Java XMLOutputFactory.createXMLEventWriter怎麽用?Java XMLOutputFactory.createXMLEventWriter使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.xml.stream.XMLOutputFactory的用法示例。


在下文中一共展示了XMLOutputFactory.createXMLEventWriter方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testXMLStreamWriter

import javax.xml.stream.XMLOutputFactory; //導入方法依賴的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());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:25,代碼來源:XMLEventWriterTest.java

示例2: testCreateStartDocument_DOMWriter

import javax.xml.stream.XMLOutputFactory; //導入方法依賴的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());
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:22,代碼來源:XMLStreamWriterTest.java

示例3: testCreateStartDocument

import javax.xml.stream.XMLOutputFactory; //導入方法依賴的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\""));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:21,代碼來源:XMLStreamWriterTest.java

示例4: testSAXResult1

import javax.xml.stream.XMLOutputFactory; //導入方法依賴的package包/類
@Test
public void testSAXResult1() {
    DefaultHandler handler = new DefaultHandler();

    try {
        SAXResult saxResult = new SAXResult(handler);
        XMLOutputFactory ofac = XMLOutputFactory.newInstance();
        XMLEventWriter writer = ofac.createXMLEventWriter(saxResult);
    } catch (Exception e) {
        if (e instanceof UnsupportedOperationException) {
            // expected
        } else {
            e.printStackTrace();
            Assert.fail(e.toString());
        }
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:18,代碼來源:Bug6846132Test.java

示例5: testEventWriterWithStAXResultNStreamWriter

import javax.xml.stream.XMLOutputFactory; //導入方法依賴的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());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:29,代碼來源:StreamResultTest.java

示例6: testEventWriterWithStAXResultNEventWriter

import javax.xml.stream.XMLOutputFactory; //導入方法依賴的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());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:29,代碼來源:StreamResultTest.java

示例7: testStreamWriterWithStAXResultNEventWriter

import javax.xml.stream.XMLOutputFactory; //導入方法依賴的package包/類
@Test
public void testStreamWriterWithStAXResultNEventWriter() throws Exception {
    try {
        XMLOutputFactory ofac = XMLOutputFactory.newInstance();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        XMLEventWriter writer = ofac.createXMLEventWriter(buffer);
        StAXResult res = new StAXResult(writer);
        XMLStreamWriter swriter = ofac.createXMLStreamWriter(res);
        Assert.fail("Expected an Exception as XMLStreamWriter can't be created " + "with a StAXResult which has EventWriter.");
    } catch (Exception e) {
        System.out.println(e.toString());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:14,代碼來源:StreamResultTest.java

示例8: testMerge

import javax.xml.stream.XMLOutputFactory; //導入方法依賴的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());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:82,代碼來源:XMLEventWriterTest.java

示例9: test

import javax.xml.stream.XMLOutputFactory; //導入方法依賴的package包/類
@Test
public void test() throws Exception {
    final String XML = "" + "<?xml version='1.0'?>" + "<doc xmlns:foo='http://example.com/foo/' xml:lang='us-en'><p>Test</p></doc>";

    javax.xml.parsers.SAXParserFactory saxFactory = javax.xml.parsers.SAXParserFactory.newInstance();

    javax.xml.parsers.SAXParser parser = saxFactory.newSAXParser();

    XMLOutputFactory outFactory = XMLOutputFactory.newInstance();
    XMLEventWriter writer = outFactory.createXMLEventWriter(System.out);

    SAX2StAXEventWriter handler = new SAX2StAXEventWriter(writer);

    InputStream is = new StringBufferInputStream(XML);

    parser.parse(is, handler);

    // if it doesn't blow up, it succeeded.
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:20,代碼來源:Bug6551616.java


注:本文中的javax.xml.stream.XMLOutputFactory.createXMLEventWriter方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。