简体   繁体   English

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

[英]while writing XML with stax parser if exception occurs in my own method then how to store or access previous data

In example below when we enter into writeOneMoreElement() method and Exception occurs then how to access previous data that we write on XML. 在下面的示例中,当我们进入writeOneMoreElement()方法并且发生Exception时,那么如何访问我们在XML上编写的先前数据。 Here we loose every entry if exception occurs on writeOneMoreElement() method. 如果writeOneMoreElement()方法发生异常,我们将在此处松开每个条目。

public class xmlSample {
  public static void main(String[] args) {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    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();

        writeOneMoreElement(writer2);

        writer2.writeEndDocument();
        writer2.close();
        xtw.flush();
        xtw.close();

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

    } catch (XMLStreamException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

private static XMLStreamWriter2 writeOneMoreElement(XMLStreamWriter2 writer2)
        throws XMLStreamException, IOException {

    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();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return writer2;
 }
}

One of the rules of Exceptions is that whichever line throws an Exception, all lines after that will not be executed until you reach the catch and finally blocks. 异常的规则之一是,无论哪一行抛出异常,之后的所有行都不会执行,直到您到达catchfinally阻塞为止。 So this line System.out.println("XML :" + new String(byteArrayOutputStream.toByteArray())); 因此,此行System.out.println("XML :" + new String(byteArrayOutputStream.toByteArray())); is never executed, but the values are still available in byteArrayOutputStream before the program ends. 永远不会执行,但是在程序结束之前,这些值在byteArrayOutputStream中仍然可用。

One possible solution is to run the same println() method in the catch block again. 一种可能的解决方案是在catch块中再次运行相同的println()方法。 Like so: 像这样:

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;
 }

Run the program, and you will get the expected XMLStreamException as well the incomplete data. 运行该程序,您将获得预期的XMLStreamException以及不完整的数据。

XML :<?xml version='1.1' encoding='UTF-8'?><document><data1>Sagar</data1><ABC><data2 name2="value2" otherAttribute="true" /><data3 name3="value3" otherAttribute="true" /></ABC>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM