Java 在使用stax解析器编写XML时,如果在我自己的方法中发生异常,那么如何存储或访问以前的数据

Java 在使用stax解析器编写XML时,如果在我自己的方法中发生异常,那么如何存储或访问以前的数据,java,xml,stax,Java,Xml,Stax,在下面的示例中,当我们进入writeOnMemorement()方法并发生异常时,我们将如何访问在XML上编写的以前的数据。在这里,如果WriteOnMemorement()方法发生异常,我们将丢失每个条目 异常规则之一是,无论哪一行抛出异常,在到达catch和finally块之前,都不会执行该异常之后的所有行。所以这一行System.out.println(“XML:+新字符串(byteArrayOutputStream.toByteArray())永远不会执行,但在程序结束之前,byteAr

在下面的示例中,当我们进入writeOnMemorement()方法并发生异常时,我们将如何访问在XML上编写的以前的数据。在这里,如果WriteOnMemorement()方法发生异常,我们将丢失每个条目


异常规则之一是,无论哪一行抛出异常,在到达
catch
finally
块之前,都不会执行该异常之后的所有行。所以这一行
System.out.println(“XML:+新字符串(byteArrayOutputStream.toByteArray())永远不会执行,但在程序结束之前,
byteArrayOutputStream
中的值仍然可用

一种可能的解决方案是在
catch
块中再次运行相同的
println()
方法。像这样:

public static void main(String[] args) {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    //Initializing the ByteArrayOutputStream outside the try block, so that it is still available after that scope.
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try {

        XMLStreamWriter writer1 = factory.createXMLStreamWriter(new FileWriter("E:\\sampleXML.xml"));
        //ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        XMLOutputFactory xmlOutputFactory = new WstxOutputFactory();
        XMLStreamWriter2 xtw = (XMLStreamWriter2) new WstxOutputFactory()
                .createXMLStreamWriter(byteArrayOutputStream, "UTF-8");

        xtw.writeStartDocument("UTF-8", "1.1");
        xtw.setPrefix("itm", "http://adt.cmn.xmlns.commons.platform.actiance.com/core/1.0");

        xtw.writeStartElement("document");
        xtw.writeStartElement("data1");
        xtw.writeCharacters("Sagar");
        xtw.writeEndElement();

        XMLStreamWriter2 writer2 = writeOneMoreElement(xtw);

        writer2.writeStartElement("data2");
        writer2.writeCharacters("Shubham");
        writer2.writeEndElement();

        //Exception thrown here
        writeOneMoreElement(writer2);

        //Unexecuted code from here onwards
        writer2.writeEndDocument();
        writer2.close();
        xtw.flush();
        xtw.close();

        System.out.println("XML :" + new String(byteArrayOutputStream.toByteArray()));

    } catch (XMLStreamException e) {
        e.printStackTrace();
        /*Proof of concept. This line will print out all the values inserted into the ByteArrayOutputStream up to
        the point where the Exception was thrown.*/
        System.out.println("XML :" + new String(byteArrayOutputStream.toByteArray()));
    } catch (IOException e) {
        e.printStackTrace();
    }

}

private static XMLStreamWriter2 writeOneMoreElement(XMLStreamWriter2 writer2)
        throws XMLStreamException/*, IOException*/ {
    //By the way, why does this method throw IOException? It's unnecessary, and you can remove it.
    try {
        writer2.writeStartElement("ABC");
        writer2.writeStartElement("data2");
        writer2.writeAttribute("name2", "value2");
        writer2.writeAttribute("otherAttribute", "true");
        writer2.writeEndElement();

        writer2.writeStartElement("data3");
        writer2.writeAttribute("name3", "value3");
        writer2.writeAttribute("otherAttribute", "true");
        writer2.writeEndElement();

        writer2.writeEndElement();

        writer2.flush();
        //I'm throwing an Exception here on purpose to trigger the catch block.
        throw new XMLStreamException();
    }       
    catch (Exception e) {
        e.printStackTrace();
        // I'm rethrowing the Exception on purpose.
        throw e;
    } 
    //This line won't work for now, but you can change it back.
    //return writer2;
 }
运行该程序,您将获得预期的
XMLStreamException
以及不完整的数据


XML:Sagar

您好,如果您觉得答案有帮助,请接受,或者评论为什么没有帮助。这有助于所有其他开发人员将来阅读本文。
public static void main(String[] args) {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    //Initializing the ByteArrayOutputStream outside the try block, so that it is still available after that scope.
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try {

        XMLStreamWriter writer1 = factory.createXMLStreamWriter(new FileWriter("E:\\sampleXML.xml"));
        //ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        XMLOutputFactory xmlOutputFactory = new WstxOutputFactory();
        XMLStreamWriter2 xtw = (XMLStreamWriter2) new WstxOutputFactory()
                .createXMLStreamWriter(byteArrayOutputStream, "UTF-8");

        xtw.writeStartDocument("UTF-8", "1.1");
        xtw.setPrefix("itm", "http://adt.cmn.xmlns.commons.platform.actiance.com/core/1.0");

        xtw.writeStartElement("document");
        xtw.writeStartElement("data1");
        xtw.writeCharacters("Sagar");
        xtw.writeEndElement();

        XMLStreamWriter2 writer2 = writeOneMoreElement(xtw);

        writer2.writeStartElement("data2");
        writer2.writeCharacters("Shubham");
        writer2.writeEndElement();

        //Exception thrown here
        writeOneMoreElement(writer2);

        //Unexecuted code from here onwards
        writer2.writeEndDocument();
        writer2.close();
        xtw.flush();
        xtw.close();

        System.out.println("XML :" + new String(byteArrayOutputStream.toByteArray()));

    } catch (XMLStreamException e) {
        e.printStackTrace();
        /*Proof of concept. This line will print out all the values inserted into the ByteArrayOutputStream up to
        the point where the Exception was thrown.*/
        System.out.println("XML :" + new String(byteArrayOutputStream.toByteArray()));
    } catch (IOException e) {
        e.printStackTrace();
    }

}

private static XMLStreamWriter2 writeOneMoreElement(XMLStreamWriter2 writer2)
        throws XMLStreamException/*, IOException*/ {
    //By the way, why does this method throw IOException? It's unnecessary, and you can remove it.
    try {
        writer2.writeStartElement("ABC");
        writer2.writeStartElement("data2");
        writer2.writeAttribute("name2", "value2");
        writer2.writeAttribute("otherAttribute", "true");
        writer2.writeEndElement();

        writer2.writeStartElement("data3");
        writer2.writeAttribute("name3", "value3");
        writer2.writeAttribute("otherAttribute", "true");
        writer2.writeEndElement();

        writer2.writeEndElement();

        writer2.flush();
        //I'm throwing an Exception here on purpose to trigger the catch block.
        throw new XMLStreamException();
    }       
    catch (Exception e) {
        e.printStackTrace();
        // I'm rethrowing the Exception on purpose.
        throw e;
    } 
    //This line won't work for now, but you can change it back.
    //return writer2;
 }